TC-1 Code to Write¶
Be sure to read Flex and Bison documentations and tutorials, see Flex & Bison.
See the lecture notes, and read the C++ chapter of
GNU Bison’s documentation. Note that the version being used for the
Tiger project may differ from the latest public release, thus
students should build their own documentation
by running make html in the provided Bison tarball.
Pay special attention to its ‘’Complete C++ Example’’ which is very much like our set up.
- Makefile.am
Include your own test suite in the
testsdirectory, and hook it tomake check.- src/parse/scantiger.ll
The scanner must be completed to read strings, identifiers, etc. and track locations.
If the environment variable
SCANis defined (to whatever value) Flex scanner debugging traces are enabled, i.e. set the variableyy_flex_debugto 1.Strings will be stored as C++
std::string. See the following code for the basics.... "\"" grown_string.clear(); BEGIN SC_STRING; <SC_STRING>{ /* Handling of the strings. Initial " is eaten. */ "\"" { BEGIN INITIAL; return TOKEN_VAL(STRING, grown_string); } ... \\x[0-9a-fA-F]{2} { grown_string.append(1, strtol(yytext + 2, 0, 16)); } ... }Symbols (i.e. identifiers) must be returned as
misc::symbolobjects, not strings.The locations are tracked. The class
Locationto use is produced by Bison:src/parse/location.hh.To track locations, adjust your scanner, use
YY_USER_ACTIONand theyylexprologue:... %% %{ // Everything here is run each time :code:`yylex` is *invoked*. %} /* The rules. */ "if" return TOKEN(IF); ... %%
- src/parse/parsetiger.yy
The grammar must be complete but without actions.
Use
%skeleton "lalr1.cc"to have Bison generate a LALR(1) parser.Use
%expect 0to have Bison report conflicts are genuine errors.Use the environment variable
PARSEto enable parser traces, i.e. to setyydebugto 1, run:PARSE=1 tc foo.tig
Use
%printerto implement--parse-tracesupport for terminals (see TC-1 Samples). For instance,%define api.value.type variant %token <int> INT "integer" %printer { yyo << $$; } <int>
- src/parse/tiger-parser.cc
The class
TigerParserdrives the lexing and parsing of input file. Its implementation insrc/parse/tiger-parser.ccis incomplete.TigerParseracts as a bridge between the lexer, the parser and the error handling, among other things. For more information, see old/02-parser-scanner.pdf: most of what you need to know is detailed here.- lib/misc/symbol.*, lib/misc/unique.*
The class
misc::symbolkeeps a single copy of identifiers, see The lib/misc Directory. Its implementation inlib/misc/symbol.hxxandlib/misc/symbol.ccis incomplete. Note that runningmake checkinlib/miscexerciseslib/misc/test-symbol.cc: having this unit test pass should be a goal by itself. As a matter of fact, unit tests were left to help you: once they pass successfully you may proceed to the rest of the compiler.misc::symbol’s implementation is based onmisc::unique, a generic class implementing the Flyweight design pattern. The definition of this class,lib/misc/unique.hxx, is also to be completed.- lib/misc/variant.*
The implementation of the class template
misc::variant<T0, Ts...>lacks a couple of conversion operators that you have to supply.