I am making a compiler with flex, bisonc++ and gcc (in Ubuntu), which compile a simple esoteric programming language to c++ source code(I don't want to make assembly code). I want to make optimization as well, so I need an AST to do it.
I also have a symbol table, but I don't have any idea how to construct an AST properly and if I have a correct AST for the grammar, how to make code optimization (so I don't want just print the AST). My grammar (.y file) is full and correct (it recognize every syntax error).
It's clear that I have to write the action code of AST into the .y file, but I don't know what to read (I mentioned it also have syntax table), and where I should define my AST struct or class.
My files:
language.l
(lex file where are the tokens)
language.y
(bisonc++ source file(the grammar of my language))
lex.yy.cc
(it's generated by flex)
Those are generated by bisonc++:
Parser.h
(in this file, I added the symbol table, which is an std::map<std::string, var_data>
where var_data
is struct defined in semantics.h
)
Parser.ih
Parserbase.h
parse.cc
I have a language.cc
file which containts the main
fuction, it reads the input file (comand line argument) and starts the analysis.
I also have a semantics.h
header which contains structs for the symbol table.