1 / 40

FlexCup 起步

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)

madison
Download Presentation

FlexCup 起步

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FlexCup起步 贺天行

  2. First please download to appetitzer in our webpage • This tutorial will be mainly about codes provided in the appetizer

  3. Files you need

  4. 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)

  5. What Flex does is generating tokens i = i + 2 ; ID(i) EQUAL ID(i) PLUS NUMBER(2) SEMI

  6. To use Flex you need to know Regular Expressions • See Jflex manual to see the definition RE. Example: Digits=[0-9]*

  7. Book Reader Time! • Can any RE be converted to an NFA? & can any NFA be converted to a DFA?

  8. Tips about JFlex • Use .bat to help you • Now we just run jflex.bat 1.flex to generate

  9. We can also use Makefile

  10. Flex&symbols.java • 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.Symbolso we can use this to test our Scanner.

  12. Test your Scanner • ScannerTest(download).

  13. 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.

  14. Flex : escape character • What does \\there mean?

  15. 接下来进入文法部分

  16. Regular Expression is limited • How to use RE to express ((()))?

  17. What do we want from cup?A parse(yu3fa1) tree(su4)!

  18. Concepts for Grammar Context free grammar

  19. Use a grammar to express ((())) F=(F)|epilson

  20. 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)?

  21. 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

  22. 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

  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.

  24. shift/reduce action • Shift : move in another token • Reduce : use a production rule

  25. A grammar

  26. First : write out states in the parse table

  27. Second : use some rules to construct the parse table

  28. A typical parse table

  29. How grammar***(*) like SLR(1) is defined? • Algorithm 4.46

  30. Use the parse table to parse

  31. 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

  32. Tips about JCup

  33. 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

  34. Also useful to include position info(help you debug)

  35. Use prettierParser to give prettier parsing treeDownload the code in wiki

  36. Add classname into the output • Every class derives from Stmt, so we add this line • Then we get className in the output

  37. 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.

  38. The output in ScannerTest and ParserTest will show you what results you get from Flex and Cup. They can greatly help your debugging.

  39. 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

More Related