flexcup 起步

Post on 25-Feb-2016

95 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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) Yylex.java(generaged by flex) Parser.java(generated by cup)

What Flex does is generating tokensi = 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

We can also use Makefile

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 \\t here mean?

接下来进入文法部分

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

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

Concepts for Grammar Context free grammar

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

Book Reader Time!

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

A grammar

First : write out states in the parse table

Second : use some rules to construct the parse table

A typical parse table

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

Use the parse table to parse

Book Reader Time! Given a grammar G, what is

FOLLOW(A) for nonterminal A?

Tips about JCup

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

Also useful to include position info(help you debug)

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 theoutput

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.

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

top related