yacc. yacc 2 yacc takes a description of a grammar as its input and generates the table and code for...

14
Yacc Yacc

Upload: roland-long

Post on 19-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc

Page 2: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 22

Yacc takes a description of a grammar Yacc takes a description of a grammar as its input and generates the table and as its input and generates the table and code for a LALR parser.code for a LALR parser.

Input specification file is in 3 parts Input specification file is in 3 parts – Declarations and Definitions Declarations and Definitions – Grammar and Actions Grammar and Actions – User-Written code User-Written code

Parts are separated by %% Parts are separated by %%

Page 3: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 33

GrammarGrammar

We will start with the grammar section, We will start with the grammar section, since this is the easiest to relate to what since this is the easiest to relate to what you are learning.you are learning.

ProductionsProductions – Grammars are defined in near-BNF form. The Grammars are defined in near-BNF form. The

differences can be summarized as follows: differences can be summarized as follows:

– 1. Single characters used as terminals are put 1. Single characters used as terminals are put into single quote, but nonterminals are written into single quote, but nonterminals are written out by name. out by name.

Page 4: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 44

– 2. Terminals that are keywords, or tokens 2. Terminals that are keywords, or tokens like like idid are declared as such in the are declared as such in the declarations section. declarations section.

– 3. Instead of 3. Instead of in the production, Yacc in the production, Yacc uses a colon, but alternatives are uses a colon, but alternatives are separated by a | as usual. separated by a | as usual.

– 4. Yacc uses a blank to represent an 4. Yacc uses a blank to represent an epsilon production.epsilon production.

Page 5: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 55

Thus a grammar like Thus a grammar like E -> E+T | E-T | TE -> E+T | E-T | T

T -> T*F | T/F | FT -> T*F | T/F | F

F -> (E) | IF -> (E) | I

can be written as: can be written as: expr : expr '+' termexpr : expr '+' term

| expr '-' term| expr '-' term

| term| term

;;

Page 6: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 66

term : term '*' factterm : term '*' fact

| term '/' fact| term '/' fact

| fact| fact

;;

fact : '(' expr ')'fact : '(' expr ')'

| ID| ID

;;

In this example, ID will have been In this example, ID will have been declared a token in the declarations part.declared a token in the declarations part.

Page 7: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 77

Semantic ActionsSemantic Actions – Inside of { } you can have code segments. Inside of { } you can have code segments. – Each item in the production has a semantic Each item in the production has a semantic

value. value. $$ is the left hand side, $$ is the left hand side, things on the RHS are numbered from $1 on. things on the RHS are numbered from $1 on.

Thus Thus expr : expr '+' term { $$ = $1 + $3; }expr : expr '+' term { $$ = $1 + $3; }

If the attributes are structs we can have If the attributes are structs we can have expr : ID { $$.loc = $1.loc; }expr : ID { $$.loc = $1.loc; }

Page 8: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 88

Declarations and DefinitionsDeclarations and Definitions

In the declarations section we identify all In the declarations section we identify all tokens except the single-character tokens except the single-character operators operators

(unless they are also returned as a token). (unless they are also returned as a token).

To declare a token we write: To declare a token we write: – %token ID%token ID– %token NUMBER%token NUMBER

Page 9: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 99

Yacc assigns a numerical code to each Yacc assigns a numerical code to each token, and expects these codes to be token, and expects these codes to be returned to it by the lexical analyzer.returned to it by the lexical analyzer.

– This assignment is placed in yytab.h This assignment is placed in yytab.h

– you can get Lex to use these by placing you can get Lex to use these by placing #include "yytab.h"#include "yytab.h"

inside the %{ %} at the beginning of inside the %{ %} at the beginning of your Lex specification. your Lex specification.

Page 10: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 1010

Notice you do not have to declare non-Notice you do not have to declare non-terminals. terminals.

– Their appearances on the left-hand side of Their appearances on the left-hand side of productions in the grammar section productions in the grammar section declares them automatically. declares them automatically.

Page 11: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 1111

You can declare precedence, and You can declare precedence, and associativity.associativity.– Most of the time this is unnecessary, since Most of the time this is unnecessary, since

precedence and associativity are built into precedence and associativity are built into the grammar the grammar IFIF it is unambiguous. it is unambiguous.

Finally we must identify the starting Finally we must identify the starting symbol of the grammar. symbol of the grammar. – %start statement%start statement

Page 12: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 1212

The data type for attributes has the The data type for attributes has the predefined name YYSTYPE, and we predefined name YYSTYPE, and we must define what it means. must define what it means.

%{%{

#include <stdio.h>#include <stdio.h>

#typedef int YYSTYPE;#typedef int YYSTYPE;

%}%}

Note: this allows you to change what Note: this allows you to change what YYSTYPE is by declaring a struct and YYSTYPE is by declaring a struct and using it.using it.

Page 13: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 1313

User Written CodeUser Written Code

The user written code contains (at a minimum) The user written code contains (at a minimum) the main program (that invokes the parser) and the main program (that invokes the parser) and an error handler.an error handler.main(){main(){

yyparse();yyparse();

}}

void yyerror(char * msg){void yyerror(char * msg){

printf("%s\n", msg);printf("%s\n", msg);

}}

Page 14: Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts

YaccYacc 1414

A Sample Yacc SpecificationA Sample Yacc Specification

Examples...Examples...