





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
Material Type: Notes; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Fall 2002;
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Context-sensitive analysis What context-sensitive questions might the compiler ask?
These cannot be answered with a context-free grammar
CMSC 430 Lecture 6, Page 1
Context-sensitive analysis Why is context-sensitive analysis hard?
How can we answer these questions?
Attribute grammars
Attribute grammar
Dependences between attributes
CMSC 430 Lecture 6, Page 3
Example attribute grammar
A grammar to evaluate signed binary numbers due to Scott K. Warren, Rice Ph.D.
Production Evaluation Rules 1 NUM ::= SIGN LIST LIST .pos โ 0 NUM .val โ if SIGN.neg then - LIST .val else LIST .val 2 SIGN ::= + SIGN.neg โ false 3 SIGN ::= - SIGN.neg โ true 4 LIST ::= BIT BIT .pos โ LIST .pos LIST .val โ BIT .val 5 LIST 0 ::= LIST 1 BIT LIST 1 .pos โ LIST 0 .pos + 1 BIT .pos โ LIST 0 .pos LIST 0 .val โ LIST 1 .val + BIT .val 6 BIT ::= 0 BIT .val โ 0 7 BIT ::= 1 BIT .val โ 2 BIT^ .pos
Abstract syntax tree
An abstract syntax tree (AST) is the procedureโs parse tree with the nodes for most non-terminal symbols removed.
<id,x> *
<num, 2 > <id,y>
This represents โx - 2 * yโ.
For ease of manipulation, can use a linearized (operator) form of the tree.
x 2 y * - in postfix form.
A popular intermediate representation.
CMSC 430 Lecture 6, Page 7
Symbol tables
A symbol table associates values or attributes (e.g., types and values) with names.
What should be in a symbol table?
What information might compiler need?
Symbol tables
Implementation
How to handle nested lexical scoping?
One solution
CMSC 430 Lecture 6, Page 9
Type systems
Types
Type system
Example type rules
Advantages of typed languages
A simple type checker
Using a synthesized attribute grammar, we will describe a type checker for arrays, pointers, statements, and functions.
Grammar for source language:
P ::= D ; E D ::= D ; E | id: T T ::= char | integer | array [num] of T | โ T E ::= literal | num | id | E mod E | E[E] | E โ
CMSC 430 Lecture 6, Page 13
Type checking example
Partial attribute grammar for the type system
D ::= id: T { addtype(id.entry, T.type) } T ::= char { T.type โ char } T ::= integer { T.type โ integer } T ::= โ T 1 { T.type โ pointer(T 1 .type) } T ::= array [num] of T 1 { T.type โ array( 1.. .num.val, T 1 .type) }
Type checking expressions
Each expression is assigned a type using rules associated with the grammar.
E ::= literal { E.type โ char } E ::= num { E.type โ integer } E ::= id { E.type โ lookup(id.entry) } E ::= E 1 mod E 2 { E.type โ if E 1 .type = integer and E 2 .type = integer then integer else typeError } E ::= E 1 [E 2 ] { E.type โ if E 2 .type = integer and E 1 .type = array(s,t) then t else typeError } E ::= E 1 โ { E.type โ if E 1 .type = pointer then t else typeError }
CMSC 430 Lecture 6, Page 15
Type checking statements
Statements do not typically have values, therefore we assign them the type void. If an error is detected within the statement, it gets type typeError.
S ::= id โ E { S.type โ if id.type = E.type then void else typeError } S ::= if E then S 1 { S.type โ if E.type = boolean then S 1 .type else typeError } S ::= while E do S 1 { S.type โ if E.type = boolean then S 1 .type else typeError } S ::= S 1 ; S 2 { S.type โ if S 1 .type = void then void else typeError }