javaparsingexpressiongrammarcup

java - cup parser allow me to do empty transitions in a grammar LALR?


hello guys i am new in cup parser, my problem is that in the define of trans in my grammar i have empty trans and my question is, is that correct?

for example, in my grammar i have

INIT -> A B -c-   
A -> A -a-  
| empty  
B -> B -b-  
| empty  

with A, B as no terminals and a, b, c as terminals

i tried to make a alternative gramar but is extend for the language that i try to parse without the empty trans

how can i program that in cup to make the parser? thanks for all your contributions and help...


Solution

  • For cup grammar examples, you can check out the site here.

    Regarding your grammar, you'd have to first define your terminals and non terminals

    Terminal: a, b, c, EMPTY;
    
    Non-Terminal: init, A,B;
    

    And then start your grammar

    start with: init
    
    init ::= A:e | B:e;
    A    ::= A:e | A:e -a- | EMPTY;
    B    ::= B:e | B:e -b- | EMPTY;
    a    ::= a:e {: RESULT = e:}
    b    ::= b:e {: RESULT = e:}
    

    and so on. Keep in mind with this method, you're going to have to have some way to define your terminals and symbols. If you're looking for something to use as a lexer, I'd recommend jflex.