02-07syntaxDirectedTranslation

02-07syntaxDirectedTranslation - 1 CSE 450: Compilers K....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 450: Compilers K. Stirewalt Parsing and syntax analysis Part 7: Syntax-directed translation Topics: symbols and actions in yacc syntax-directed definitions of computations synthesized and inherited attributes S-attributed and L-attributed SDDs Readings: Read Section 5.1 and 5.2 in Dragon Book CSE 450: Compilers K. Stirewalt Topics covered thus far Conceptual architecture of a compiler Lexical analysis Syntax analysis Context free grammars and derivations Predictive parsing Bottom-up parsing Grammar design CSE 450: Compilers K. Stirewalt Syntax-directed translation Method of language translation accomplished by attaching rules or program fragments to productions in a grammar Based on two concepts: Associate data attributes (or values) with symbols in a grammar Augment grammar productions with program fragments Fragments executed when a production is used Combined execution of fragments, in order induced by the parse, effects a translation of the program being compiled CSE 450: Compilers K. Stirewalt Examples Generating intermediate code: Values are sequences of intermediate code Program fragments generate sequences for composite language constructs by combining sequences generated for the parts Evaluating an expression: Values are integers that represent the result of evaluating the expression Program fragments perform the evaluation recursively, as dictated by the major arithmetic operator in a production CSE 450: Compilers K. Stirewalt Symbol values in yacc Every symbol in a yacc parser has a value gives additional information about the symbol e.g., if symbol is a terminal that represents a numeric literal, the value might be the numeric value of the literal when it was scanned Values may be referenced and defined inside actions in a yacc grammar E.g., A: X Y Z { $$ = f($1, $2, $3); } Here, $$ refers to the value of the symbol A It is computed in terms of the values of symbols X ($1), Y ($2), and Z ($3). CSE 450: Compilers K. Stirewalt Example %token LITERAL %left + - %left * %nonassoc UMINUS %% expr: expr + expr | expr - expr | expr * expr | - expr %prec UMINUS | ( expr ) | LITERAL; %% Suppose we wish for our parser to evaluate the expression while its parsing Need to: associate a value with the LITERAL tokens associate a value with expression non-terminals combine these values when reducing productions 2 CSE 450: Compilers K. Stirewalt Example %token <v> LITERAL %left + - %left * %nonassoc UMINUS %% expr: expr + expr | expr - expr | expr * expr | - expr %prec UMINUS | ( expr )...
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 7

02-07syntaxDirectedTranslation - 1 CSE 450: Compilers K....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online