in reply to Re: Parse grammar via RecDescent parser
in thread Parse grammar via RecDescent parser

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.

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

    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".

        Maybe you will need to adapt your logic then.

        If you want two things, but your other code can only handle a single thing, one or the other has to give.

        My first approach would be to only call eval on the first thing if an arrayref is returned.

        Other rules doing && or ! or || on this result would not be affected by this, but they might also need to be adapted to cope with the result not being a single string.

        Let me note that you still have not shown us any workable example of what you have and what you want which is not amenable to a simple change like the one I outlined. My personal recommendation from having written half-assed parsers/program generators is to separate parsing the code from generating the output. Most approaches work on the resulting parse tree to restructure the tree according to simple rules and only after the (one or more pass off) restructuring generate the code from the tree.

Re^3: Parse grammar via RecDescent parser
by Anonymous Monk on May 17, 2016 at 15:02 UTC

    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.

        If you're creating Perl code in your parser, why do you worry about non-Perl languages?

        If you need to have a non-static but singular object, maybe you want to implement the Singleton pattern in your language of choice. Or maybe you want to provide the target container using Dependency Injection.

        Or maybe you can help us help you better by telling us more about your overarching goal. I have the impression that you try to achieve many things from a single and oddly specific approach, and that approach seems to be a bad fit for the solutions you try to get from it.