40
FlexCup 起起 起起起

FlexCup 起步

  • Upload
    madison

  • View
    83

  • Download
    4

Embed Size (px)

DESCRIPTION

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) - PowerPoint PPT Presentation

Citation preview

Page 1: FlexCup 起步

FlexCup起步贺天行

Page 2: FlexCup 起步

First please download to appetitzer in our webpage

This tutorial will be mainly about codes provided in the appetizer

Page 3: FlexCup 起步

Files you need

Page 4: FlexCup 起步

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)

Page 5: FlexCup 起步

What Flex does is generating tokensi = i + 2 ;

ID(i) EQUAL ID(i) PLUS NUMBER(2) SEMI

Page 6: FlexCup 起步

To use Flex you need to know Regular Expressions See Jflex manual to see the definition

RE.Example:Digits=[0-9]*

Page 7: FlexCup 起步

Book Reader Time! Can any RE be converted to an NFA?

& can any NFA be converted to a DFA?

Page 8: FlexCup 起步

Tips about JFlex Use .bat to help you

Now we just run jflex.bat 1.flex to generate

Page 9: FlexCup 起步

We can also use Makefile

Page 10: FlexCup 起步

Flex&symbols.java Symbols.java is just a

mapping(token2int) table. It enables Yylex to pass tokens:%implements Symbols"(" { return tok(LPAREN); }

Page 11: FlexCup 起步

The scanner will generate series of java_cup.runtime.Symbolso we can use this to test our Scanner.

Page 12: FlexCup 起步

Test your Scanner ScannerTest(download).

Page 13: FlexCup 起步

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.

Page 14: FlexCup 起步

Flex : escape character What does \\t here mean?

Page 15: FlexCup 起步

接下来进入文法部分

Page 16: FlexCup 起步

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

Page 17: FlexCup 起步

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

Page 18: FlexCup 起步

Concepts for Grammar Context free grammar

Page 19: FlexCup 起步

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

Page 20: FlexCup 起步

Book Reader Time!

Given a context-free gramar G,What’s the language L(G)?

Page 21: FlexCup 起步

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

Page 22: FlexCup 起步

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

Page 23: FlexCup 起步

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.

Page 24: FlexCup 起步
Page 25: FlexCup 起步

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

Page 26: FlexCup 起步

A grammar

Page 27: FlexCup 起步

First : write out states in the parse table

Page 28: FlexCup 起步

Second : use some rules to construct the parse table

Page 29: FlexCup 起步

A typical parse table

Page 30: FlexCup 起步

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

Page 31: FlexCup 起步

Use the parse table to parse

Page 32: FlexCup 起步

Book Reader Time! Given a grammar G, what is

FOLLOW(A) for nonterminal A?

Page 33: FlexCup 起步

Tips about JCup

Page 34: FlexCup 起步

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

Page 35: FlexCup 起步

Also useful to include position info(help you debug)

Page 36: FlexCup 起步

Use prettierParser to give prettier parsing treeDownload the code in wiki

Page 37: FlexCup 起步

Add classname into the output Every class derives from Stmt, so we

add this line

Then we get className in theoutput

Page 38: FlexCup 起步

Think : how does these work?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.

Page 39: FlexCup 起步

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

Page 40: FlexCup 起步

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