javayacclexjflexcup

Is there a way to display the current parsing action using CUP and Jflex?


For my compilers class, we had to create a Lexical Analyzer and a Syntax Analyzer using JFlex and CUP. Part of the assignment also requires us to print out the tokens and corresponding parsing actions for a given input file. Additionally, the reduce actions need to display the corresponding production number.

For example, if my parser's grammar was

  1. S -> aXc
  2. X -> bX
  3. X -> b
  4. X -> Yd
  5. Y -> Yd
  6. Y -> d

Then a string of abbbc given as input would output

a [shift]
b [shift]
b [shift]
b [shift]
c [reduce 3] [reduce 2] [reduce 2] [shift]
[reduce 1]
[accept]

I know that CUP has the debug option, but the output needs to be in this specific format. Displaying the tokens was easy because I could just print out the tokens in the rules section of the Flex specification, but I can't figure out how to print out the actions in the CUP specification.


Solution

  • Answering this myself in case anyone else runs into the same issue- I ended up changing my call to CUP from parser() to parser_debug(), which displays all of the information about the shifts and reduces, and had my lexical analyzer print out the tokens. The output worked out where I was able to redirect the output of both applications, and pipe it in a python script I wrote that reformats things into the way the professor wanted.