Abstract Syntax - lgxZJ/Tiger-Compiler GitHub Wiki
This phase convert tiger language syntax into AST, an abstract Syntax Tree. Also, take a tiger language snippet for example:
var varName : int := 0
The code above declares a variable named varName
to be of type int and initialize its value to zero. It belongs to a long form syntax of variable declarations permitted by tiger. In this phase, we convert syntax like this into data structures like below:
struct myLongFormVar_
{
mySymbol name;
mySymbol type;
myExp value;
};
We collect information about the name, type and value of this variable into a special data structure, note that the plain texts var
, :
and :=
are omitted because of their uselessness. Other syntax is alike.
Generally, we gather useful information and throw away others.
-
Better readability and maintainability : If used Yacc before, you will know that this tool provide a way for users to do semantic actions for the syntax defined. But it would looks like a big mass if you do all the things needed in that place, codes will become hard to read. And poor readability causes poor maintainability eventually.
-
Free order to traverse : Yacc is a recursive descent LALR(1) parser, its syntax parsing order is fixed. With AST, users will get more free to traverse the abstract syntax while losing as less information as possible.
-
Better modularity : Provided AST, syntax analysis and semantic analysis are separated which improves modularity.
AST, standing for Abstract Syntax Tree, is an abstraction of language syntax. Actually, in this project, they are represented by C structures.
Definitions and constructors of AST:
myAbstractSyntax.h
abstractSyntaxMaker.h
abstractSyntaxMaker.c
Debug printers for AST since they are difficult to tests :
myPrintAbstractSyntax.h
myPrintAbstractSyntax.c
Places where AST creation occurs:
myTiger.y