Troubleshooting Parse Error for Flex and Bison

In summary, the speaker is struggling with creating an assignment statements parser using Lex and Yacc (or their variants Flex and Bison). They have encountered a "parse error" when trying to parse tokens and have found that removing the return statements in their Lex rules solves the issue, but then loses functionality needed for Yacc. They ask for help and later realize that the issue was caused by different token type values in the generated C file from Bison on their Windows machine and the Unix machine they were running the program on. This was due to copying #defines from the C file to a header file, which was made on the Windows machine and did not match the Unix version.
  • #1
Dissident Dan
238
2
I've got an assignment for a class in which I'm supposed to make an assignment statements parser using Lex and Yacc...well, actually variants called Flex and Bison.

I am having major problems getting this to work. It will recognize one token or character and then give a "parse error".

I noticed that if I remove the return statements from my Lex rules, it is able to continue parsing without giving me the dreaded "parse error", but then I lose the functionality that I need for Yacc to be able to parse the tokens.

Does anyone have any idea of what's going on?
 
Computer science news on Phys.org
  • #2
Would it be possible to post your lex file?
 
  • #3
I should have done that.

Anyway, I figured it out. As always, it was something really simple and stupid. I am working on a Windows machine, but then I upload my files to Unix machine and run the program there.

As you know, you have to copy the #defines for the token types from the C file generated by Bison (Yacc) to a header file that is included by the Flex (Lex) file. Well, It turns out that the sharp define values are different by 1 between the Windows version of Bison that I am using on my own comp and the Unix version on the remote PC. I made the header file from the C file generated on my computer, so when I tried to run it on Unix, the #defines didn't match, and that gave me the parse errors.
 

Related to Troubleshooting Parse Error for Flex and Bison

What is a parse error?

A parse error occurs when the syntax of a program written in a specific language (such as Flex or Bison) does not match the expected grammar rules of that language. This can result in the program being unable to properly interpret or execute the code.

What causes a parse error for Flex and Bison?

A parse error for Flex and Bison can be caused by a variety of factors, such as typos or missing punctuation in the code, using incorrect grammar rules, or attempting to parse an invalid input.

How can I troubleshoot a parse error for Flex and Bison?

To troubleshoot a parse error for Flex and Bison, you can start by carefully reviewing your code for any obvious errors or inconsistencies. You can also use debugging tools and techniques, such as stepping through the code and checking the values of variables, to identify the source of the error.

Can a parse error be fixed?

Yes, a parse error for Flex and Bison can often be fixed by correcting the errors in the code. In some cases, you may need to make changes to the grammar rules or input to properly parse the code.

How can I prevent parse errors in my Flex and Bison programs?

To prevent parse errors in your Flex and Bison programs, it is important to carefully follow the syntax and grammar rules of the language. Additionally, regularly testing and debugging your code can help catch any potential errors before they become a problem.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Sticky
  • Engineering and Comp Sci Homework Help
Replies
1
Views
19K
  • Sticky
  • Biology and Chemistry Homework Help
Replies
1
Views
15K
Back
Top