Need help regarding one LALR(1) parsing.

Posted by AppleGrew on Stack Overflow See other posts from Stack Overflow or by AppleGrew
Published on 2011-06-23T10:12:14Z Indexed on 2011/06/23 16:22 UTC
Read the original article Hit count: 438

I am trying to parse a context-free language, called Context Free Art. I have created its parser in Javascript using a YACC-like JS LALR(1) parser generator JSCC.

Take the example of following CFA (Context Free Art) code. This code is a valid CFA.

startshape A
rule A { CIRCLE { s 1} }

Notice the A and s in above. s is a command to scale the CIRCLE, but A is just a name of this rule. In the language's grammar I have set s as token SCALE and A comes under token STRING (I have a regular expression to match string and it is at the bottom of of all tokens).

This works fine, but in the below case it breaks.

startshape s
rule s { CIRCLE { s 1} }

This too is a perfectly valid code, but since my parser marks s after rule as SCALE token so it errors out saying that it was expecting STRING.

Now my question is, if there is any way to re-write the production rules of the parser to account for this? The related production rule is:-

rule:
    RULE STRING '{' buncha_replacements '}'    [* rule(%2, 1) *]
    |
    RULE STRING RATIONAL '{' buncha_replacements '}'  [* rule(%2, 1*%3) *]
    ;

One simple solution I can think of is create a copy of above rule with STRING replaced by SCALE, but this is just one of the many similar rules which would need such fixing. Furthermore there are many other terminals which can get matched to STRING. So that means way too many rules!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about lalr