Parsing grammar error with JCup and JFlex when run parser

350 Views Asked by At

Continuing with the example that I am doing with JCup and JFlex Error when run parsing using cup and jflex, I am getting an error when parsing the following statement.

Label(name="Label1", title="Label title");

I don't understand the reason for the error, but I have looked for examples of how it does it in java15.cup. I understand that it is not the same, but it seems to be an approximation. When the Lexer is executed, when showing the tokens found by console, it does not print the COMMA token, I do not understand the reason if it has the same SEMMICOLON configuration. I think there is something in the grammar that I don't understand and so I ask for help to see if I can solve it or find another solution.

Macro declaration on jflex file

LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
WhiteSpace     = {LineTerminator}+ | [ ,\t,\f]+

/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} | {DocumentationComment}

TraditionalComment   = "/*" [^*] ~"*/" | "/*" "*"+ "/"
// Comment can be the last line of the file, without line terminator.
EndOfLineComment     = "//" {InputCharacter}* {LineTerminator}?
DocumentationComment = "/**" {CommentContent} "*"+ "/"
CommentContent       = ( [^*] | \*+ [^/*] )*

/* General identifiers */
Identifier          = [A-Za-z_$] [A-Za-z_$0-9]*

StringConst         = \" ([^\"])* \"

/*
    identifiers
*/
ComponentId     = \" {Identifier} \"

/*
    string to assign values, title, etc
*/
StringLit           = {StringConst}

Some Terminal

"["             { return iSymbol(Token.LBRCKT.name(), Symbol.LBRCKT, yytext()); }
"]"             { return iSymbol(Token.RBRCKT.name(), Symbol.RBRCKT, yytext()); }
";"             { return iSymbol(Token.SEMICOLON.name(), Symbol.SEMICOLON, yytext()); }
","             { return iSymbol(Token.COMMA.name(), Symbol.COMMA, yytext()); }
"."             { return iSymbol(Token.DOT.name(), Symbol.DOT, yytext()); }
"="             { return iSymbol(Token.ASSIGN.name(), Symbol.ASSIGN, yytext()); }
":"             { return iSymbol(Token.COLON.name(), Symbol.COLON, yytext()); }
"("             { return iSymbol(Token.LPAREN.name(), Symbol.LPAREN, yytext()); }
")"             { return iSymbol(Token.RPAREN.name(), Symbol.RPAREN, yytext()); }
"{"             { return iSymbol(Token.LCBRACE.name(), Symbol.LCBRACE, yytext()); }
"}"             { return iSymbol(Token.RCBRACE.name(), Symbol.RCBRACE, yytext()); }

The grammar defined for the statement is as follows;

name_property_def   ::= NAME ASSIGN STRING_ID ;

title_property_def  ::= TITLE ASSIGN STRING_LIT ;


label_basic_declaration ::=
        IQ_LABEL LPAREN label_formal_parameter_list_opt RPAREN SEMICOLON 
    ;

label_formal_parameter_list_opt ::=
    |   label_formal_parameter_list
    ;
    
label_formal_parameter_list ::=
        label_formal_parameter_list COMMA label_formal_parameter
    |   label_formal_parameter
    ;
    
label_formal_parameter ::=
        name_property_def
    |   title_property_def
    ;

the values that go between parentheses are fixed, that is, they are the names of the "properties" that this object has. I appreciate the help

Lexer trace 
Token: Symbol: LABEL (unknown:11/2(-1) - unknown:11/8(-1)) Label 
Token: Symbol: LPAREN (unknown:11/9(-1) - unknown:11/9(-1)) (
Token: Symbol: NAME (unknown:11/10(-1) - unknown:11/13(-1)) name
Token: Symbol: ASSIGN (unknown:11/14(-1) - unknown:11/14(-1)) = 
Token: Symbol: STRING_ID (unknown:11/15(-1) - unknown:11/24(-1)) "Label1"
Token: Symbol: TITLE (unknown:11/26(-1) - unknown:11/30(-1)) title
Token: Symbol: ASSIGN (unknown:11/31(-1) - unknown:11/31(-1)) = 
Token: Symbol: STRING_LIT (unknown:11/32(-1) - unknown:11/46(-1)) "Label title"
Token: Symbol: RPAREN (unknown:11/47(-1) - unknown:11/47(-1)) )
Token: Symbol: SEMICOLON (unknown:11/48(-1) - unknown:11/48(-1)) ;

# Initializing parser
# Current Symbol is #19
# Shift under term #19 to state #2
# Current token is Symbol: LPAREN (unknown:2/8(-1) - unknown:2/8(-1))
# Shift under term #3 to state #5
# Current token is Symbol: NAME (unknown:2/9(-1) - unknown:2/12(-1))
# Shift under term #33 to state #21
# Current token is Symbol: ASSIGN (unknown:2/13(-1) - unknown:2/13(-1))
# Shift under term #11 to state #25
# Current token is Symbol: STRING_ID (unknown:2/14(-1) - unknown:2/23(-1))
# Shift under term #15 to state #26
# Current token is Symbol: RPAREN (unknown:2/24(-1) - unknown:2/24(-1))
# Reduce with prod #10 [NT=6, SZ=3]
# Reduce rule: top state 5, lhs sym 6 -> state 40
# Goto state #40
# Reduce with prod #6 [NT=4, SZ=1]
# Reduce rule: top state 5, lhs sym 4 -> state 39
# Goto state #39
# Shift under term #4 to state #41
# Current token is Symbol: LCBRACE (unknown:2/26(-1) - unknown:2/26(-1))
# Reduce with prod #4 [NT=5, SZ=3]
# Reduce rule: top state 2, lhs sym 5 -> state 6
# Goto state #6
# Shift under term #5 to state #7
# Current token is Symbol: LABEL (unknown:11/2(-1) - unknown:11/8(-1))
# Shift under term #21 to state #9
# Current token is Symbol: LPAREN (unknown:11/9(-1) - unknown:11/9(-1))
# Shift under term #3 to state #15
# Current token is Symbol: NAME (unknown:11/10(-1) - unknown:11/13(-1))
# Shift under term #33 to state #21
# Current token is Symbol: ASSIGN (unknown:11/14(-1) - unknown:11/14(-1))
# Shift under term #11 to state #25
# Current token is Symbol: STRING_ID (unknown:11/15(-1) - unknown:11/24(-1))
# Shift under term #15 to state #26
# Current token is Symbol: TITLE (unknown:11/27(-1) - unknown:11/31(-1))
Compiler has detected a syntax error at line 0 column 0
# Attempting error recovery
# Finding recovery state on stack
# Pop stack by one, state was # 26
# Pop stack by one, state was # 25
# Pop stack by one, state was # 21
# Pop stack by one, state was # 15
# Pop stack by one, state was # 9
# Pop stack by one, state was # 7
# Pop stack by one, state was # 6
# Pop stack by one, state was # 2
# Pop stack by one, state was # 0
# No recovery state found on stack
# Error recovery fails
Couldn't repair and continue parse for input symbol "TITLE" spanning from unknown:11/27(-1) to unknown:11/31(-1)

It could be that the StringLit or ComponentId macro is causing the problem.

0

There are 0 best solutions below