Syntax Analysis - lgxZJ/Tiger-Compiler GitHub Wiki

Syntax Analysis

What does this phase do?

Actually, syntax analysis begins almost the same time as lexical analysis since we use Lex and Yacc to do the real work and they works simultaneously.

But suppose we have done lexical analysis, we now have many valid tokens. With these tokens, we can compose a lot of statements. However, among these statements, some are meaningful, some are not. Again, we take C language statements for an example:

int var = 0;
int 0 = var;

The two statements listed above are all formed with the same four valid tokens: int, var, =, 0. The first one is a valid statement which declares a int variable var in C context, but the second is not.

That is what this phase do, it analyze the token sequence to see whether it forms some valid statement. For details about tiger language syntax, see Tiger Language Reference Manual page of this wiki.

Interface

  • do.h
  • do.c

Users can use interfaces provided in these files to do syntax analyzing.

Tools used

Here, as described in tiger book, i used Yacc to do the syntax analyze work of this compiler.

Note

While using Yacc to implement this phase, i found the most difficult thing is to design a grammar without ambiguity. Fortunately, this has already been done by the author.(-_- thanks to Andrew W. Appel)

Sources

Yacc files:

  • myTiger.y

Special files:

  • y.ouput : An output file generated when parsing the grammar specified in the Yacc file. It points out grammar conflicts, usually some shift/reduce errors. If your output shows there is any reduce/reduce errors, then your grammar is probably poorly designed.
  • y.tab.c : The syntax analyzer, or parser, generated by running Yacc with command yacc -dv myTiger.y.
  • y.tab.h : The header containing Yacc-used data types generated with y.tab.c.
⚠️ **GitHub.com Fallback** ⚠️