Programming Languages (CSC-302 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

# Outline of Class 9: Attribute Grammars

Held: Friday, February 6, 1998

• Randy Smith of Torrent Systems, Inc., a "Parallel Software Infrastructure Company", will give a talk entitled The Programming They Don't Teach You In College on Monday, February 9, as part of the noon brown-bag lunch series.
• Read Chapter 6 of Louden for Monday.
• Don't forget that assignment 2 is due today. Some of you have been sick, so I won't start grading it until next week.

## Attribute Grammars

• How do we use grammars in building and describing programming languages? Initially,
• To indicate the proper form of utterances in the language.
• As the basis of building parsers for the language.
• However, we can also use grammars to translate or otherwise perform computation on the strings we translate.
• What are some things we might compute?
• The value of an expression.
• The code for a program.
• The type for a variable.
• ...
• A common way to tie this computation to the grammar is what is known as an attribute grammar.
• In attribute grammars, you associate attributes (values) with the nonterminals and terminals in the language.
• How do you compute those values? With rules that we attach to each production. Each rule can involve the symbols on both sides of the production (in practice, there may be restrictions on rules.)
• We'll visit a number of simple examples.

### An Attribute Grammar for Evaluating Expressions

• For our expression grammar, an appropriate computation will be the value of the expression that is parsed.
• What attributes will we need?
• Expressions, terms, factors, and numbers will all need a value.
• We'll skip variables for now, as their values are dependent on larger program issues.
• Operators will need associated functions.
• Some of the rules are easy to determine, as they just involve copying values.
```Exp ::= Term
Exp.value = Term.value
Term ::= Factor
Term.value = Factor.value
Factor ::= '(' Exp ')'
Factor.value = Exp.value
```

• For the operations, we assign a function depending on the base string.
```AddOp ::= '+'
MulOp ::= '*'
MulOp.function = multiply
MulOp ::= '/'
MulOp.function = divide
```

• We can now return to the more complex rules. Since the same nonterminals appear multiple times, we'll number them so that we can distinguish tem.
```Exp_0 ::= Exp_1 AddOp Term
Term_0 ::= Term_1 MulOp Factor
```

• We'll work with a few simple examples.

### An Attribute Grammar for Compiling Conditionals

• We can even use attribute grammars to generate code (most often, assembly code).
• We'll consider the simple case of generating simple assembly code for a conditional.
• We'll assume that each segment of code has a length (so that we can do relative jumps, which I'll mark with `RELJMP`.
• We may also want to generate a list of error messages.
• You'll note that this is getting closer and closer to the code we might write in some language.
```Statement ::= if Exp then Statement else Statement
// Verify the type and initialize the list of errors
if Exp.type != Bool {
newerrs = [Statement_0.line, "Must use booleans in conditionals"]
}
else {
}
// Generate the code from the various components.
Statement_0.code =
Exp.code +
[RELJMPNONZERO Exp.loc length(Statement_1.code)+1] +
Statement_1.code +
[RELJMP length(Statement_2.code)] +
Statement_2.code;
Statement_0.errs =
```

On to Attribute Grammars, Continued
Back to The Deadly Conditional Grammar
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Current position in syllabus

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.