in reply to Parse grammar via RecDescent parser

Hi I would use arrays or a hash if I could. I would have to specify that array in the grammar which would mean that it would be inside the parser's namespace and cannot be accessed from outside. I am looking for some way I could store the data in the parser itself and then retrieve later after all statements in the file have been tokenized.
  • Comment on Re: Parse grammar via RecDescent parser

Replies are listed 'Best First'.
Re^2: Parse grammar via RecDescent parser
by Corion (Patriarch) on May 17, 2016 at 13:43 UTC

    You know that you can get the parse tree from Parse::RecDescent with the <autotree> directive? This should give you all the data that is available in the structure and content of your source file.

    Maybe you can show us a short, workable example of maybe 20-35 lines that shows what you have and where you are trying to get to?

Re^2: Parse grammar via RecDescent parser
by GotToBTru (Prior) on May 17, 2016 at 16:30 UTC

    Why would you need to specify the array in the grammar? Your plan is to store the data in the parser.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re^2: Parse grammar via RecDescent parser
by the_dark_lord (Novice) on May 17, 2016 at 14:17 UTC

    Hi

    My grammar rule looks like the following:

    file_exists: 'file(' filename ')' { $return = "-e $item{ 'filename' } }
    Now I would want this rule to do two things: 1) return the string "-e < filename>" so that I can eval it later and check existence of that file 2) store the file names which I can retrieve later

    I can achieve only one of the two from the grammar that I have. I want to do both.

      Why not store both, the string and the filename?

      file_exists: 'file(' filename ')' { $return = [ "-e $item{ 'filename' }", $item{ 'filename' +}] }

        That is an issue because I am calling 'eval' on the string returned. Also, this rule can only return the string as mentioned because it can be used in other rules doing && or ! or || operations on this. Therefore this rule can return only "-e <filename".

      guessing...

      file_exists: 'file(' filename ')' { $return = "-e $item{ 'filename' }; push @somepackagename +::savedfilenames, $item{ 'filename' } }

      Then outside the parser, retrieve the array as @somepackagename::savedfilenames

        I had thought of that but the file names are to be stored in a non-static object. Providing a static method for a accessing a non-static object is not a good design. Perl allows this but it wont be allowed for other languages such as Java.