Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax Directed Translation

Similar presentations


Presentation on theme: "Syntax Directed Translation"— Presentation transcript:

1 Syntax Directed Translation
Professor Yihjia Tsai Tamkang University

2 Phases of a Compiler 1. Lexical Analyzer (Scanner)
Takes source Program and Converts into tokens 2. Syntax Analyzer (Parser) Takes tokens and constructs a parse tree. 3. Semantic Analyzer Takes a parse tree and constructs an abstract syntax tree with attributes.

3 Phases of a Compiler- Contd
Syntax Directed Translation 4. Takes an abstract syntax tree and produces an Interpreter code (Translation output) 5. Intermediate-code Generator Takes an abstract syntax tree and produces un- optimized Intermediate code.

4 Motivation: Parser as Translator
Syntax-directed translation Parser ASTs, byte code assembly code, etc Stream of tokens Syntax + translation rules (often hardcoded in the parser)

5 Important Syntax directed translation: attaching actions to the grammar rules (productions). The actions are executed during the compilation (not during the generation of the compiler, not during run time of the program!). Either when replacing a nonterminal with its rhs (LL, top-down) or a handle with a nonterminal (LR, bottom-up). The compiler-compiler generates a parser which knows how to parse the program (LR,LL). The actions are “implanted” in the parser and are executed according to the parsing mechanism.

6 Example :Expressions E  E + T E  T T  T * F T  F F  ( E ) F  num

7 Synthesized Attributes
The attribute value of the terminal at the left hand side of a grammar rule depends on the values of the attributes on the right hand side. Typical for LR (bottom up) parsing. Example: TT*F {$$.val=$1.val$3.val}. T.val T.val F.val

8 Example :Expressions In LEX
E  E + T {$$.val:=$1.val+$3.val;} E  T {$$.val:=$1.val;} T  T * F {$$.val:=$1.val*$3.val;} T  F {$$.val:=$1.val;} F  ( E ) {$$.val:=$2.val;} F  num {$1.val:=$1.val;}

9 Example 2:Type definitions
D  T L T  int T  real L  id , L L  id

10 Inherited attributes The value of the attributes of one of the symbols to the right of the grammar rule depends on the attributes of the other symbols (left or right). Typical for LL parsing (top down). D  T {$2.type:=$1.type} L L  id , {$3.type:=$1.type} L D.type L.type T.type L.type id , L.type

11 Type definitions D  T {$2.type:=$1.type} L T  int {$$.type:=int;}
T  real {$$:=real;} L  id , L {gen(id.name,$$.type); $3.type:=$$.type;} L  id {gen(id.name,$$.type); } T.type int

12 Type definitions: LL(1)
D  T {$2.type:=$1.type} L T  int {$$.type:=int;} T  real {$$:=real;} L  id {gen(id.name,$$.type); $2.type:=$$.type;} R R  , id {gen(id.name,$$.type); } R   T.type int

13 How to arrange things for LL(1) on stack?
Include on the stack, except for the grammar symbol also the actions, and a shadow copy for each nonterminal. Each time one sees an action on the stack, execute it. Shadow copies are used to get synthesized values and pass them further to the right of the rule.

14 LR parser LR(k) parser action goto
Given the current state on top and current token, consult the action table. Either, shift, i.e., read a new token, put in stack, and push new state, or or Reduce, i.e., remove some elements from stack, and given the newly exposed top of stack and current token, to be put on top of stack, consult the goto table about new state on top of stack. a + b $ LR(k) parser sn X0 sn-1 Xn-1 s0 action goto

15 LR parser adapted. LR(k) parser action goto a + b $ Attributes
Same as before, plus: Whenever reduce step, execute the action associated with grammar rule. If left-to right inherited attributes exist, can also execute actions in middle of rule. Can put record of attributes, associated with a grammar symbol, on stack. LR(k) parser sn X0 sn-1 Xn-1 s0 action goto

16 LL parser LL(k) parser Parsing table $ Z X Y a + b
If top symbol X a terminal, must match current token m. If not, pop top of stack. Then look at table T[X, m] and push grammar rule there in reverse order.

17 $ 2+3*4$ $num +3*4$ Fnum F.type:=2 $F TF T.type:=2 $T ET E.type:=2
num.type:=2 $num +3*4$ Fnum F.type:=2 $F TF T.type:=2 $T ET E.type:=2 $E shift $E+ 3*4$ num.type:=3 $E+num *4$ F.type:=3 $E+F $E+T $E+T* 4$ num.type:=4 $E+T*num F.type:=4 $E+T*F TT*F T.type:=12 EE*T E.type:=14

18 LL parser Adapted LL(k) parser Parsing table
If top symbol X a terminal, must match current token m. Put actions into stack as part of rules. Hold for each nonterminal a record with attributes. If nonterminal, replace top of stack with shadow copy. Then look at table T[X, m] and push grammar rule there in reverse order. If shadow copy, remove. This way nonterminal can deliver values down and up. a + b $ Attributes LL(k) parser Y X Z $ Actions Parsing table

19 On stack to be read rule action $D int a,b$ $(D)L{}T DT{}L Tint{}
$(D)L{}(T)int{} Tint{} $(D)L{}(T) a,b$ T.type:=int $(D)L L.type:=int $(D)(L)R{}id Lid{}R $(D)(L)R ,b$ Gen(a,int), R.type:=int $(D)(L)(R){}id, R, id $(D)(L)(R){}id b$ $(D)(L)(R) $ Gen(b,int),

20 Expressions in LL: Eliminating left recursion
E  E + T E  T T  T * F T  F F  ( E ) F  num E  T E’ E’  + T E’ E’   T  F T’ T’  * F T’ T’   F  ( E ) F  num

21 (2+3)*4  E  T E’ E’  + T E’ E’   T  F T’ T’  * F T’ T’  
F  ( E ) F  num 3

22 Actions in LL      E T E’ F T’ ( E ) F T’ * T E’ 4 F T’ + T E’ 2
E  T {$2.down:=$1.up;} E’ {$$.up:=$2.up;} E’  + T {$3.down:=$$.down+$2.up;} E’ {$$.up:=$3.up;} E’   {$$.up:=$$.down;} T  F {$2.down:=$1.up;} T’ {$$.up:=$2.up;} T’  * F {$3.down:=$$.down+$2.up;} T’ {$$.up:=$3.down;} T’   {$$.up:=$$.down;} F  ( E ) {$$.up:=$2.up;} F  num {$$.up:=$1.up;} T E’ F T’ ( E ) F T’ * T E’ 4 F T’ + T E’ 2 F T’ 3

23 Syntax Directed Translation Scheme
A syntax directed translation scheme is a syntax directed definition in which the net effect of semantic actions is to print out a translation of the input to a desired output form. This is accomplished by including “emit” statements in semantic actions that write out text fragments of the output, as well as string-valued attributes that compute text fragments to be fed into emit statements.

24 Syntax-Directed Translation
Values of these attributes are evaluated by the semantic rules associated with the production rules. Evaluation of these semantic rules: may generate intermediate codes may put information into the symbol table may perform type checking may issue error messages may perform some other activities in fact, they may perform almost any activities. An attribute may hold almost any thing. a string, a number, a memory location, a complex record. Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent.

25 Syntax-Directed Definitions and Translation Schemes
When we associate semantic rules with productions, we use two notations: Syntax-Directed Definitions Translation Schemes

26 Schemes Syntax-Directed Definitions: Translation Schemes:
give high-level specifications for translations hide many implementation details such as order of evaluation of semantic actions. We associate a production rule with a set of semantic actions, and we do not say when they will be evaluated. Translation Schemes: indicate the order of evaluation of semantic actions associated with a production rule. In other words, translation schemes give a little bit information about implementation details.

27 Syntax-Directed Definitions
A syntax-directed definition is a generalization of a context-free grammar in which: Each grammar symbol is associated with a set of attributes. This set of attributes for a grammar symbol is partitioned into two subsets called synthesized and inherited attributes of that grammar symbol. Each production rule is associated with a set of semantic rules. Semantic rules set up dependencies between attributes which can be represented by a dependency graph. This dependency graph determines the evaluation order of these semantic rules. Evaluation of a semantic rule defines the value of an attribute. But a semantic rule may also have some side effects such as printing a value.

28 Annotated Parse Tree A parse tree showing the values of attributes at each node is called an annotated parse tree. The process of computing the attributes values at the nodes is called annotating (or decorating) of the parse tree. Of course, the order of these computations depends on the dependency graph induced by the semantic rules.

29 Syntax-Directed Definition
In a syntax-directed definition, each production A→α is associated with a set of semantic rules of the form: b=f(c1,c2,…,cn) where f is a function and b can be one of the followings:  b is a synthesized attribute of A and c1,c2,…,cn are attributes of the grammar symbols in the production ( A→α ). OR  b is an inherited attribute one of the grammar symbols in α (on the right side of the production), and c1,c2,…,cn are attributes of the grammar symbols in the production ( A→α ).

30 Attribute Grammar So, a semantic rule b=f(c1,c2,…,cn) indicates that the attribute b depends on attributes c1,c2,…,cn. In a syntax-directed definition, a semantic rule may just evaluate a value of an attribute or it may have some side effects such as printing values. An attribute grammar is a syntax-directed definition in which the functions in the semantic rules cannot have side effects (they can only evaluate values of attributes).

31 Syntax-Directed Definition -- Example
Production Semantic Rules L → E return print(E.val) E → E1 + T E.val = E1.val + T.val E → T E.val = T.val T → T1 * F T.val = T1.val * F.val T → F T.val = F.val F → ( E ) F.val = E.val F → digit F.val = digit.lexval Symbols E, T, and F are associated with a synthesized attribute val. The token digit has a synthesized attribute lexval (it is assumed that it is evaluated by the lexical analyzer).

32 Annotated Parse Tree -- Example
Input: 5+3*4 L E.val= return E.val= T.val=12 T.val= T.val=3 * F.val=4 F.val= F.val= digit.lexval=4 digit.lexval= digit.lexval=3

33 Dependency Graph L Input: 5+3*4 E.val=17 E.val=5 T.val=12
T.val= T.val= F.val=4 F.val= F.val= digit.lexval=4 digit.lexval= digit.lexval=3

34 Syntax-Directed Definition – Example2
Production Semantic Rules E → E1 + T E.loc=newtemp(), E.code = E1.code || T.code || add E1.loc,T.loc,E.loc E → T E.loc = T.loc, E.code=T.code T → T1 * F T.loc=newtemp(), T.code = T1.code || F.code || mult T1.loc,F.loc,T.loc T → F T.loc = F.loc, T.code=F.code F → ( E ) F.loc = E.loc, F.code=E.code F → id F.loc = id.name, F.code=“” Symbols E, T, and F are associated with synthesized attributes loc and code. The token id has a synthesized attribute name (it is assumed that it is evaluated by the lexical analyzer). It is assumed that || is the string concatenation operator.

35 Syntax-Directed Definition – Inherited Attributes
Production Semantic Rules D → T L L.in = T.type T → int T.type = integer T → real T.type = real L → L1 id L1.in = L.in, addtype(id.entry,L.in) L → id addtype(id.entry,L.in) Symbol T is associated with a synthesized attribute type. Symbol L is associated with an inherited attribute in.

36 A Dependency Graph – Inherited Attributes
Input: real p q D L.in=real T L T.type=real L1.in=real addtype(q,real) real L id addtype(p,real) id.entry=q id id.entry=p parse tree dependency graph

37 Syntax Trees Decoupling Translation from Parsing-Trees.
Syntax-Tree: an intermediate representation of the compiler’s input. Example Procedures: mknode, mkleaf Employment of the synthesized attribute nptr (pointer) PRODUCTION SEMANTIC RULE E  E1 + T E.nptr = mknode(“+”,E1.nptr ,T.nptr) E  E1 - T E.nptr = mknode(“-”,E1.nptr ,T.nptr) E  T E.nptr = T.nptr T  (E) T.nptr = E.nptr T  id T.nptr = mkleaf(id, id.lexval) T  num T.nptr = mkleaf(num, num.val)

38 Draw the Syntax Tree a-4+c id to entry for c id num 4 to entry for a

39 Directed Acyclic Graphs for Expressions
a + a * ( b – c ) + ( b – c ) * d + * - a b c d

40 Examples 1. Postfix and Prefix notations:
We have already seen how to generate them. Let us generate Java Byte code. E -> E’+’ E { emit(“iadd”);} E-> E ‘* ‘ E { emit(“imul”);} E-> T T -> ICONST { emit(“sipush ICONST.string);} T-> ‘(‘ E ‘)’

41 Abstract Stack Machine
The abstract machine code for an expression simulates a stack evaluation of the postfix representation for the expression. Expression evaluation proceeds by processing the postfix representation from left to right.

42 Evaluation 1. Pushing each operand onto the stack when encountered.
2. Evaluating a k-ary operator by using the value located k-1 positions below the top of the stack as the leftmost operand, and so on, till the value on the top of the stack is used as the rightmost operand. 3. After the evaluation, all k operands are popped from the stack, and the result is pushed onto the stack (or there could be a side-effect)

43 Example Stmt -> ID ‘=‘ expr { stmt.t = expr.t || ‘istore a’}
Applied to a = 3*b –c bipush 3 iload b imul iload c isub istore a

44 Java Virtual Machine Analogous to the abstract stack machine, the Java Virtual machine is an abstract processor architecture that defines the behavior of Java Bytecode programs. The stack (in JVM) is referred to as the operand stack or value stack. Operands are fetched from the stack and the result is pushed back on to the stack. Advantages: VM code is compact as the operands need not be explicitly named.

45 Data Types The int data type can hold 32 bit signed integers in the range -2^31 to 2^(31) -1. The long data type can hold 64 bit signed integers. Integer instructions in the Java VM are also used to operate on Boolean values. Other data types that Java VM supports are byte, short, float, double. (Your project should handle at least three data types).

46 Selected Java VM Instructions
Java VM instructions are typed i.e., the operator explicitly specifies what operand types it expects. Expression Evaluation sipush n push a 2 byte signed int on to stack iload v load/push a local variable v istore v store top of stack onto local var v iadd pop two elements and push their sum isub pop two elements and push their difference

47 Selected Java VM Instructions
imul pop two elements and push their product iand pop two elements and push their bitwise and ior pop two elements and push their bitwise or ineg pop top element and push its negation lcmp pop two elements (64 bit integers), push the comparison result. 1 if Vs[0]<vs[1], 0 if vs[0]=vs[1] otherwise -1. i2l convert integers to long l2i convert long to integer

48 Selected Java VM Instructions
Branches: GOTO L unconditional transfer to label l ifeq L transfer to label L if top of stack is 0 ifne L transfer to label L if top of stack !=0 Call/Return: Each method/procedure has memory space allocated to hold local variables (vars register), an operand stack (optop register) and an execution environment (frame register)

49 Selected Java VM Instructions
Invokestatic p invoke method p. pop args from stack as initial values of formal parameters (actual parameters are pushed before calling). Return return from current procedure ireturn return from current procedure with integer value on top of stack. Areturn return from current procedure with object reference return value on top of stack.

50 Selected Java VM Instructions
Array Manipulation: Java VM has an object data type reference to arrays and objects newarray int create a new array of integers using the top of the stack as the size. Pop the stack and push a reference to the newly created array. Iaload pop array subscript expression on top of stack and array pointer (next stack element). Push value contained in this array element. iastore

51 Selected Java VM Instructions
Object Manipulation new c create a new instance of class C (using heap) and push the reference onto stack. getfield f push value from object field f of object pointed by object reference at the top of stack. putfield f store value from vs[1] into field f of object pointed by the object reference vs[0]

52 Selected Java VM Instructions
Simplifying Instructions: ldc constant is a macro which will generate either bipush or sipush depending on c.

53 Byte Code (JVM Instructions)
No-arg operand: (instructions needing no arguments hence take only one byte.) examples: aaload, aastore,aconsta_null, aload_0, aload_1, areturn, arraylength, astore_0, athrow, baload, iaload, imul etc One-arg operand: bipush, sipush,ldc etc methodref op: invokestatic, invokenonvirtual, invokevirtual

54 Byte Code (JVM Instructions)
Fieldref_arg_op: getfield, getstaic, putfield, pustatic. Class_arg_op: checkcast, instanceof, new labelarg_op (instructions that use labels) goto, ifeq, ifne, jsr, jsr_w etc Localvar_arg_op: iload, fload, aload, istore

55 Translating an If statement
Stmt -> if expr then stmt1 { out = newlabel(); stmt.t = expr.t|| ‘ifnnonnull’ || out || stmt1.t || ‘label’ out: ‘nop’ } example: if ( a +90==7) { x = x+1; x = x+3;}

56 Translating a while statement
Stmt -> WHILE (expr) stmt1 { in =newlabel(); out= newlabel(); emit( stmt.t = ‘label’ || in|| ‘nop’ || expr.t || ‘ifnonnull’|| out|| stmt1.t || ‘goto’ || in|| ‘label’ || out) }

57 References Compilers Principles, Techniques and Tools, Aho, Sethi, and Ullman , Chapter 5 Appel, Chapter 4 and 5


Download ppt "Syntax Directed Translation"

Similar presentations


Ads by Google