Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ......

57
CAPSL Flex/Bison Tutorial Aaron Myles Landwehr [email protected] 1 2/17/2012

Transcript of Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ......

Page 1: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex/Bison Tutorial

Aaron Myles Landwehr

[email protected]

1 2/17/2012

Page 2: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

GENERAL COMPILER OVERVIEW

2 2/17/2012

Page 3: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Compiler Overview

Lexer / Scanner

Parser Semantic Analyzer

Optimizers Code

Generator

Frontend Middle-end Backend

3 2/17/2012

Page 4: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Lexer/Scanner

• Lexical Analysis

– process of converting a sequence of characters into a sequence of tokens.

4

foo = 1 - 3**2

Lexeme Token Type

foo Variable

= Assignment Operator

1 Number

- Subtraction Operator

3 Number

** Power Operator

2 Number

2/17/2012

Page 5: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Parser

• Syntactic Analysis – The process of analyzing a sequence of tokens to determine its

grammatical structure.

– Syntax errors are identified during this stage.

5

foo

3 2

-

1 **

Lexeme Token Type

foo Variable

= Assignment Operator

1 Number

- Subtraction Operator

3 Number

** Power Operator

2 Number

=

2/17/2012

Page 6: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Semantic Analyzer

• Semantic Analysis

– The process of performing semantic checks.

• E.g. type checking, object binding, etc.

6

float a = "example"; error: incompatible types in initialization

Code: Semantic Check Error:

2/17/2012

Page 7: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Optimizer(s)

• Compiler Optimizations

– tune the output of a compiler to minimize or maximize some attributes of an executable computer program.

– Make programs faster, etc…

7 2/17/2012

Page 8: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Code Generator

• Code Generation

– process by which a compiler's code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine.

8

foo: addiu $sp, $sp, -16 addiu $2, $zero, 345 addiu $sp, $sp, 16 jr $ra

int foo() { return 345; }

2/17/2012

Page 9: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

LEX/FLEX AND YACC/BISON OVERVIEW

9 2/17/2012

Page 10: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

General Lex/Flex Information

• lex

– is a tool to generator lexical analyzers.

– It was written by Mike Lesk and Eric Schmidt (the Google guy).

– It isn’t used anymore.

• flex (fast lexical analyzer generator)

– Free and open source alternative.

– You’ll be using this.

10 2/17/2012

Page 11: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

General Yacc/Bison Information

• yacc

– Is a tool to generate parsers (syntactic analyzers).

– Generated parsers require a lexical analyzer.

– It isn’t used anymore.

• bison

– Free and open source alternative.

– You’ll be using this.

11 2/17/2012

Page 12: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Lex/Flex and Yacc/Bison relation to a compiler toolchain

12

Lexer / Scanner

Parser Semantic Analyzer

Optimizers Code

Generator

Frontend Middle-end Backend

Lex/Flex (.l spec file)

Yacc/Bison (.y spec file)

2/17/2012

Page 13: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

FLEX IN DETAIL

13 2/17/2012

Page 14: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

How Flex Works

• Flex uses a .l spec file to generate a tokenizer/scanner.

• The tokenizer reads an input file and chunks it into a series of tokens which are passed to the parser.

14

flex .l spec file lex.yy.c

2/17/2012

Page 15: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex .l specification file

15

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} /* This tells flex to read only one input file */ %option noyywrap %% /*** Rules section ***/ /* [0-9]+ matches a string of one or more digits */ [0-9]+ { /* yytext is a string containing the matched text. */ printf("Saw an integer: %s\n", yytext); } .|\n { /* Ignore all other characters. */ } %% /*** C Code section ***/

2/17/2012

Page 16: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Rule Format

16

• Matches text input via Regular Expressions

• Returns the token type.

• Format: REGEX { /*Code*/ return TOKEN-TYPE; } …

2/17/2012

Page 17: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Regex Matching Rules

• Flex matches the token with the longest match:

– Input: abc

– Rule: [a-z]+

Token: abc(not "a" or "ab")

• Flex uses the first applicable rule:

– Input: post

– Rule1: "post" { printf("Hello,"); }

– Rule2: [a-zA-z]+ { printf ("World!"); }

It will print Hello, (not “World!”)

17 2/17/2012

Page 18: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

18

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

2/17/2012

Page 19: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

19

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Match one or more characters between 0-9.

Match one or more characters between 0-9.

2/17/2012

Page 20: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

20

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Store the Number. Store the Number.

2/17/2012

Page 21: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

21

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Return the token type. Declared in the .y file. Return the token type. Declared in the .y file.

2/17/2012

Page 22: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

22

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Match one or more alphabetical characters.

Match one or more alphabetical characters.

2/17/2012

Page 23: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

23

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Store the text.

Store the text.

2/17/2012

Page 24: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

24

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Return the token type. Declared in the .y file. Return the token type. Declared in the .y file.

2/17/2012

Page 25: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

25

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Match any single character

Match any single character

2/17/2012

Page 26: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Flex Example

26

[0-9]+ { /*Code*/ yylval.dval = atof(yytext); return NUMBER; } [A-Za-z]+ { /*Code*/ struct symtab *sp = symlook(yytext); yylval.symp = sp; return WORD; } . { return yytext[0]; }

Return the character. No need to create special symbol for this case.

Return the character. No need to create special symbol for this case.

2/17/2012

Page 27: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

BISON IN DETAIL

27 2/17/2012

Page 28: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

How Bison Works

• Bison uses a .y spec file to generate a parser.

• The parser reads a series of tokens and tries to determine the grammatical structure with respect to a given grammar.

28

bison .y spec file

somename.tab.c

somename.tab.h

2/17/2012

Page 29: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

What is a Grammar?

• A grammar

– is a set of formation rules for strings in a formal language. The rules describe how to form strings from the language's alphabet (tokens) that are valid according to the language's syntax.

29 2/17/2012

Page 30: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

30

Above is a simple grammar that allows recursive math operations… Above is a simple grammar that allows recursive math operations…

E E + E E - E E * E E / E id

2/17/2012

Page 31: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

31

E E + E E - E E * E E / E id

These are productions

These are productions

2/17/2012

Page 32: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

32

E E + E E - E E * E E / E id

In this case expressions (E) can be made up of the statements on the right. *Note: the order of the right side doesn’t matter.

In this case expressions (E) can be made up of the statements on the right. *Note: the order of the right side doesn’t matter.

2/17/2012

Page 33: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

33

E E + E E - E E * E E / E id

How does this work when parsing a series of tokens?

How does this work when parsing a series of tokens?

2/17/2012

Page 34: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

34

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 35: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

35

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

We start by parsing from the left. We find that we have an id.

We start by parsing from the left. We find that we have an id.

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 36: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

36

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

An id is an expression. An id is an expression.

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 37: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

37

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

Next it will match one of the rules based on the next token because the parser know 2 is an expression.

Next it will match one of the rules based on the next token because the parser know 2 is an expression.

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 38: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

38

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

The production with the plus is matched because it is the next token in the stream.

The production with the plus is matched because it is the next token in the stream.

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 39: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

39

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

Next we move to the next token which is an id and thus an expression.

Next we move to the next token which is an id and thus an expression.

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 40: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Simple Example Grammar

40

E E + E E - E E * E E / E id

Suppose we had the following tokens: 2 + 2 - 1

Suppose we had the following tokens: 2 + 2 - 1

We know that E + E is an expression. So we can apply the same ideas and move on until we finish parsing…

We know that E + E is an expression. So we can apply the same ideas and move on until we finish parsing…

2/17/2012

Lexeme Token Type

2 Number

+ Addition Operator

2 Number

- Subtraction Operator

1 Number

Page 41: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison .y specification file

41

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression %% /*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; } %% /*** C Code section ***/ 2/17/2012

Page 42: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: definition Section Example

42

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression

2/17/2012

Page 43: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: definition Section Example

43

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression

Declaration of Tokens: %token <TYPE> NAME Declaration of Tokens: %token <TYPE> NAME

2/17/2012

Page 44: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: definition Section Example

44

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression

Higher

Operator Precedence and Associativity

Operator Precedence and Associativity

Lower

2/17/2012

Page 45: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: definition Section Example

45

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression

Associativity Options: %left - a OP b OP c %right - a OP b OP c %nonassoc - a OP b OP c (ERROR)

2/17/2012

Page 46: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: definition Section Example

46

/*** Definition section ***/ %{ /* C code to be copied verbatim */ %} %token <symp> NAME %token <dval> NUMBER %left '-' '+' %left '*' '/' %type <dval> expression

Defined non-terminal name (the left side of

productions)

Defined non-terminal name (the left side of

productions)

2/17/2012

Page 47: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

47

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

2/17/2012

Page 48: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

48

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

This is the grammar for bison. It should look similar to the simple example

grammar from before.

This is the grammar for bison. It should look similar to the simple example

grammar from before.

2/17/2012

Page 49: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

49

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

What this says is that a statement list is made up of a statement OR a statement list

followed by a statement.

What this says is that a statement list is made up of a statement OR a statement list

followed by a statement.

2/17/2012

Page 50: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

50

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

The same logic applies here also. The first production is an assignment statement, the

second is a simple expression.

The same logic applies here also. The first production is an assignment statement, the

second is a simple expression.

2/17/2012

Page 51: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

51

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

This simply says that an expression is a number or

a name.

This simply says that an expression is a number or

a name.

2/17/2012

Page 52: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

52

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

This is an executable statement. These are found to the right of a production.

When the rule is matched, it is run. In this particular case, it just says to return the

value.

This is an executable statement. These are found to the right of a production.

When the rule is matched, it is run. In this particular case, it just says to return the

value.

2/17/2012

Page 53: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Bison: rules Section Example

53

/*** Rules section ***/ statement_list: statement '\n' | statement_list statement '\n' statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: NUMBER | NAME { $$ = $1->value; }

The numbers in the executable statement correspond to the tokens listed in the

production. They are numbered in ascending order.

The numbers in the executable statement correspond to the tokens listed in the

production. They are numbered in ascending order.

1 2 3

2/17/2012

Page 54: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

ABOUT YOUR ASSIGNMENT

54 2/17/2012

Page 55: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

What you need to do

• You are given a prefix calculator.

• You need to make infix and postfix versions of the calculator.

• You then need to add support for additional operators to all three calculators.

55

+ 2 4

2 + 4 2 4 +

2/17/2012

Page 56: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Hints

• Name your calculators “infix” and “postfix.”

• You don’t need to change the c code section of the .y.

• You may need to define new tokens for parts of the assignment.

56 2/17/2012

Page 57: Flex/Bison Tutorial - CAPSL - Research Laboratories · Flex/Bison Tutorial Aaron Myles Landwehr ... –process by which a compiler's code generator ... General Lex/Flex Information

CAPSL

Credit

• Wikipedia

– Most of the content is from or based off of information from here.

• Wookieepedia

– Nothing was taken from here.

– Not even this picture of Chewie.

• 2008 Tutorial

57

*

*From Wikipedia: qualifies as fair use under United States Copyright law.

2/17/2012