






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Syntax-directed translation, a method for translating languages guided by context-free grammars. It covers the concept of attaching attributes to grammar symbols and the use of semantic rules associated with productions. The document also introduces synthesized and inherited attributes, as well as examples of their usage. Likely to be useful for university students studying computer science, specifically those enrolled in a course related to compilers or programming language theory.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







CS 5300 - SJAllan 2
Translation of languages guided by context-free grammars Attach attributes to the grammar symbols
CS 5300 - SJAllan 3
In some cases we don’t have to follow this outline literally
CS 5300 - SJAllan 4
Production L ÆE n E Æ E 1 + T E Æ T T Æ T 1 * F T Æ F F Æ (E) F Æ digit
Semantic Rules print(E.val) E.val := E 1 .val + T.val E.val := T.val T.val := T 1 .val * F.val T.val := F.val F.val := E.val F.val := digit .lexval
CS 5300 - SJAllan 7
CS 5300 - SJAllan 8
Using inherited attributes to parse a declaration and add type information to the symbol table. L Æ id addtype (id.entry, L.in)
L 1 .in := L.in addtype (id.entry, L.in)
L Æ L 1 , id
T Æ real T.Type := real
T Æ int T.Type := integer
D Æ TL L.in := T.type
Production Semantic Rules
CS 5300 - SJAllan 9
D T.Type = real
real
L.in = real
L.in = real
L.In = real
id
, id
.
. , (^) id
CS 5300 - SJAllan 10
If all attributes are synthesized then a bottom-up parser can evaluate the attributes while it parses Add attribute fields to the nonterminals on the stack
Before each production apply the semantic rule associated with the production E.g. Assume the semantic rule associated with the A Æ XYZ production is : A.a := f (X.x, Y.y, Z.z)
CS 5300 - SJAllan 13
Bison maintains a semantic stack which, in tandem with the parser stack, helps Bison manipulate and determine the meaning of the program it compiles
The values in the semantic stack represent the values or meanings of all the grammar symbols
Exactly what constitutes values for the entries in the semantic stack depends heavily on the semantics of the language being compiled
CS 5300 - SJAllan 14
CS 5300 - SJAllan 15
CS 5300 - SJAllan 16
CS 5300 - SJAllan 19
The values on the semantic stack do not have to be of a single type
Bison must know which type to associate with each symbol in the grammar
CS 5300 - SJAllan 20
%union { int int_val; char *name_ptr; CONS *cons_ptr; ID *id_ptr; TYPE *ty_ptr; EXPR *exp_ptr; }
%type <int_val> INTCONSTSY IfHead %type <id_ptr> IdList FieldList