luisforque luisforque - 2 months ago 28
C Question

Bison error in parser

I am working on a Parser using Bison, and I am trying to parse something like

void Example() {}


When running, with debug, the output is:

Starting parse
Entering state 0
Reading a token: void A()
Next token is token VOID ()
Shifting token VOID ()
Entering state 1
Reducing stack by rule 98 (line 146):
$1 = token VOID ()
-> $$ = nterm return_options ()
Stack now 0
Entering state 32
Reading a token: Next token is token IDENTIFIER ()
Error detected on line 1.
Last token read: 'Example'

Error: popping nterm return_options ()
Stack now 0
Cleanup: discarding lookahead token IDENTIFIER ()
Stack now 0


This is the important part of the grammar productions:

program : function END_OF_FILE {return 0;}
function : return_options identifier formal_parameters block
return_options : identifier | VOID
identifier : letter list_E_letters
list_E_letters : list_E_letters algarism |
algarism : letter | digit
letter : 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z'
digit : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'


Can you help me on the reason the error is happening? I can't see why.

Thank you!

Answer

Your debug dump shows that a single token, named IDENTIFIER was returned by the lexer.

Your grammar rules appear to construct the identifier element from tokens for the individual letters, one character at a time, instead of a single IDENTIFIER token.