Download presentation
Presentation is loading. Please wait.
Published byProsper Thomas Modified over 8 years ago
1
贺天行
2
First please download to appetitzer in our webpage This tutorial will be mainly about codes provided in the appetizer
4
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)
5
i = i + 2 ; ID(i) EQUAL ID(i) PLUS NUMBER(2) SEMI
6
See Jflex manual to see the definition RE. Example: Digits=[0-9]*
7
Can any RE be converted to an NFA? & can any NFA be converted to a DFA?
8
Use.bat to help you Now we just run jflex.bat 1.flex to generate
10
Symbols.java is just a mapping(token2int) table. It enables Yylex to pass tokens: %implements Symbols "(" { return tok(LPAREN); }
11
The scanner will generate series of java_cup.runtime.Symbol so we can use this to test our Scanner.
12
ScannerTest(download).
13
We require to exit(1) when an error is found. Your program are not supposed to throw exception upon an compile error.
14
What does \\t here mean?\\t
16
How to use RE to express ((()))?
19
F=(F)|epilson
20
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)?
21
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
22
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
23
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.
25
Shift : move in another token Reduce : use a production rule
30
Algorithm 4.46
32
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
34
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
37
Every class derives from Stmt, so we add this line Then we get className in the output
38
selection-statement: 'if' '(' expression ')' statement ('else' statement)? precedence right ELSE; if then if then : else else at this point, we can both shift/reduce, however, we assigned ELSE a higher precedence, so it will be shifted.
39
The output in ScannerTest and ParserTest will show you what results you get from Flex and Cup. They can greatly help your debugging.
40
Like : How is Symbols.java used in flex? How do you handle string in flex? So, please carefully read the manual
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.