400 likes | 527 Views
FlexCup 起步. 贺天行. First please download to appetitzer in our webpage This tutorial will be mainly about codes provided in the appetizer. Files you need. Files you need. Jflex JCup (and the runtime mentioned above) .flex and .cup(contained in appetizer) Symbols.java(generated by cup)
E N D
FlexCup起步 贺天行
First please download to appetitzer in our webpage • This tutorial will be mainly about codes provided in the appetizer
Files you need • Jflex • JCup(and the runtime mentioned above) • .flex and .cup(contained in appetizer) • Symbols.java(generated by cup) • Yylex.java(generaged by flex) • Parser.java(generated by cup)
What Flex does is generating tokens i = i + 2 ; ID(i) EQUAL ID(i) PLUS NUMBER(2) SEMI
To use Flex you need to know Regular Expressions • See Jflex manual to see the definition RE. Example: Digits=[0-9]*
Book Reader Time! • Can any RE be converted to an NFA? & can any NFA be converted to a DFA?
Tips about JFlex • Use .bat to help you • Now we just run jflex.bat 1.flex to generate
Flex&symbols.java • Symbols.java is just a mapping(token2int) table. • It enables Yylex to pass tokens: %implements Symbols "(" { return tok(LPAREN); }
The scanner will generate series of java_cup.runtime.Symbolso we can use this to test our Scanner.
Test your Scanner • ScannerTest(download).
Flex : the err function • We require to exit(1) when an error is found. • Your program are not supposed to throw exception upon an compile error.
Flex : escape character • What does \\there mean?
Regular Expression is limited • How to use RE to express ((()))?
Use a grammar to express ((())) F=(F)|epilson
Book Reader Time! • If a1…an can be reduced to s • Q:what’s a CFG for something like ()()(((())))? • Q:among T,N,s, which is the output of Flex? Given a context-free gramar G, What’s the language L(G)?
Ambiguous Grammar • We say a grammar is ambiguous when it has more than one parse tree for some string. • a bad grammar • One way to deal with ambiguous grammar is to write a unambiguous grammar. • A good one
Ambiguous Grammar • Instead of rewriting the grammar – Use the more natural (ambiguous) grammar – Along with disambiguating declarations • Most tools allow precedence and associativity -(like cup)declarations to disambiguate grammars
For you to understand better about what cup is doing, I need to introduce a bit how a parser is constructed. • You need to read Ch4 in dragon book for complete knowledge.
shift/reduce action • Shift : move in another token • Reduce : use a production rule
How grammar***(*) like SLR(1) is defined? • Algorithm 4.46
Book Reader Time! • Given a grammar G, what is FOLLOW(A) for nonterminal A? The set of terminals a that can appear immediately to the right of A in some sentential form
Cup : Symbols.java & Symbol.java • Don’t mess these two up. The Symbol class here is to provide a unique object to hold each string literal • This unique Symbol will be useful in later phase
Use prettierParser to give prettier parsing treeDownload the code in wiki
Add classname into the output • Every class derives from Stmt, so we add this line • Then we get className in the output
Think : how does these work? selection-statement: 'if' '(' expression ')' statement ('else' statement)? precedence right ELSE; if then if then : else elseat this point, we can both shift/reduce , however, we assigned ELSE a higher precedence, so it will be shifted.
The output in ScannerTest and ParserTest will show you what results you get from Flex and Cup. They can greatly help your debugging.
Questions in phase1 CodeReview Like : • How is Symbols.java used in flex? • How do you handle string in flex? So, please carefully read the manual