project - cs5363 1 Project1: Build A Small Scanner/Parser...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cs5363 1 Project1: Build A Small Scanner/Parser Introducing Lex, Yacc, and POET cs5363 2 Project1: Building A Scanner/Parser Parse a subset of the C language Support two types of atomic values: int float Support one type of compound values: arrays Support a basic set of language concepts Variable declarations (int, float, and array variables) Expressions (arithmetic and boolean operations) Statements (assignments, conditionals, and loops) You can choose a different but equivalent language Need to make your own test cases Options of implementation (links available at class web site) Manual in C/C++/Java (or whatever other lang.) Lex and Yacc (together with C/C++) POET: a scripting compiler writing language Or any other approach you choose --- must document how to download/use any tools involved cs5363 3 This is just starting There will be two other sub-projects Type checking Check the types of expressions in the input program Optimization/analysis/translation Do something with the input code, output the result The starting project is important because it determines which language you can use for the other projects Lex+Yacc ===> can work only with C/C++ POET ==> work with POET Manual ==> stick to whatever language you pick This class: introduce Lex/Yacc/POET to you cs5363 4 lex/flex MyLex.l lex.yy.c gcc/cc lex.yy.c a.out a.out Input stream tokens Using Lex to build scanners Write a lex specification Save it in a file (MyLex.l) Compile the lex specification file by invoking lex/flex lex MyLex.l A lex.yy.c file is generated by lex Rename the lex.yy.c file if desired (> mv lex.yy.c MyLex.c) Compile the generated C file gcc -c lex.yy.c (or gcc -c MyLex.c) cs5363 5 The structure of a lex specification file Before the first %% Variable and Regular expression pairs Each name Ni is matched to a regular expression C declarations %{ typedef enum {} Tokens; %} Copied to the generated C file Lex configurations Starts with a single % After the first %% RE {action} pairs A block of C code is matched to each RE RE may contain variables defined before %% After the second %% C functions to be copied to the generated file N1 RE1 Nm REm %{ typedef enum {} Tokens; %} % Lex configurations %% P1 {action_1} P2 {action_2} Pn {action_n} %% int main() {} declar ations Token classes Help functions cs5363 6 Example Lex Specification(MyLex.l) cconst '([^\']+|\\\')' sconst \"[^\"]*\" %pointer %{ /* put C declarations here*/ %} %% foo { return FOO; } bar { return BAR; } {cconst} { yylval=*yytext;...
View Full Document

Page1 / 30

project - cs5363 1 Project1: Build A Small Scanner/Parser...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online