Docsity

Computer Language Engineering - Lecture Slides | CS 480, Study notes of Linguistics

Toggle sidebar
bg1
1
cs480(Prasad) L2Syntax 1
Computer Language Engineering
Computer Language Engineering
โ€ข How to give instructions to a computer?
โ€“Programming Languages.
โ€ข How to make the computer carryout the
instructions efficiently?
โ€“Compilers.
cs480(Prasad) L2Syntax 2
Anatomy of a Compiler
Intermediate Code Optimizer
Code Generator
Optimized Intermediate Representation
Assembly code
Intermediate Code Generator
Intermediate Representation
Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
cs480(Prasad) L2Syntax 3
What is a Lexical Analyzer?
โ€ข Examples of Token
โ€ข Operators = + - > ( { := == <>
โ€ขKeywords if while for int double
โ€ข Numeric literals 43 6.035 -3.6e10 0x13F3A
โ€ข Character literals โ€˜aโ€™ โ€˜~โ€™ โ€˜\โ€™โ€™
โ€ข String literals โ€œ3.142โ€ โ€œFallโ€ โ€œ\โ€\โ€ = emptyโ€
โ€ข Examples of non-token
โ€ข White space space(โ€˜ โ€™) tab(โ€˜\tโ€™) end-of-line(โ€˜\nโ€™)
โ€ข Comments /*this is not a token*/
Source program text Tokens
cs480(Prasad) L2Syntax 4
Lexical Analyzer in Action
โ€ข Partition input program text into sequence of
tokens, attaching corresponding attributes.
โ€“ E.g., C-lexeme โ€œ015โ€ token NUM attribute 13
โ€ข Eliminate white space and comments
f
for
o r v a r 1 = 1 0 v a r 1 < =
ID(โ€œvar1โ€) eq_op Num(10) ID(โ€œvar1โ€) leq_op
pf3
pf4
pf5

Partial preview of the text

Download Computer Language Engineering - Lecture Slides | CS 480 and more Study notes Linguistics in PDF only on Docsity!

cs480(Prasad) L2Syntax 1

Computer Language EngineeringComputer Language Engineering

โ€ข How to give instructions to a computer?

โ€“ Programming Languages.

โ€ข How to make the computer carryout the

instructions efficiently?

โ€“ Compilers.

cs480(Prasad) L2Syntax 2

Anatomy of a Compiler

Intermediate Code Optimizer

Code Generator

Optimized Intermediate Representation

Assembly code

Intermediate Code Generator

Intermediate Representation

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

Program (character stream)

What is a Lexical Analyzer?

โ€ข Examples of Token

โ€ข Operators = + - > ( { := == <>

โ€ข Keywords if while for int double

โ€ข Numeric literals 43 6.035 -3.6e10 0x13F3A

โ€ข Character literals โ€˜aโ€™ โ€˜~โ€™ โ€˜\โ€™โ€™

โ€ข String literals โ€œ3.142โ€ โ€œFallโ€ โ€œ\โ€\โ€ = emptyโ€

โ€ข Examples of non-token

โ€ข White space space(โ€˜ โ€™) tab(โ€˜\tโ€™) end-of-line(โ€˜\nโ€™)

โ€ข Comments /this is not a token/

Source program text Tokens

Lexical Analyzer in Action

โ€ข Partition input program text into sequence of

tokens, attaching corresponding attributes.

โ€“ E.g., C-lexeme โ€œ015โ€ token NUM attribute 13

โ€ข Eliminate white space and comments

f

for

o r v a r 1 = 1 0 v a r 1 < =

ID(โ€œvar1โ€) eq_op Num(10) ID(โ€œvar1โ€) leq_op

cs480(Prasad) L2Syntax 5

Syntax and Semantics of a programming

language?

โ€ข Syntax

โ€“ What is the structure of the program?

โ€“ Textual representation.

โ€ข Formally defined using context-free

grammars (Backus-Naur Formalism)

โ€ข Semantics

โ€“ What is the meaning of a program?

โ€ข Harder to give a mathematical definition.

cs480(Prasad) L2Syntax 6

Input to and output of a parser

minus_op

left_paren_op

num(123.3)

plus_op

num(23.6)

right_paren_op

Token Stream Parse Tree

Input: - (123.3 + 23.6)

Syntax Analyzer (Parser)

Example: A CFG for expressions

โ€ข Simple arithmetic expressions with + and *

โ€ข Terminals (or tokens)

โ€“ num for all the numbers

โ€“ plus_op (โ€˜+โ€™), minus_op (โ€˜-โ€™), times_op(โ€˜*โ€™),

left_paren_op(โ€˜(โ€˜), right_paren_op(โ€˜)โ€™)

โ€ข What is the grammar for all possible expressions?

Example: A CFG for expressions

โ†’

โ†’ โ€˜(โ€™ โ€˜)โ€™

โ†’ โ€˜-โ€™

โ†’ num

โ†’ โ€˜+โ€™

โ†’ โ€˜*โ€™

cs480(Prasad) L2Syntax 13

Parse Tree

โ€ข Graphical Representation of the parsed

structure

โ€ข Shows the sequence of derivations performed

โ€“ Internal nodes are non-terminals.

โ€“ Leaves are terminals.

โ€“ Each parent node is LHS and the children are

RHS of a production.

โ€ข Abstracts the details of sequencing of the rule

applications, but preserves decomposition of

a non-terminal.

Parse Tree Example

num num

num * ( )

num โ€˜*โ€™ โ€˜(โ€™ num โ€˜+โ€™ num โ€˜)โ€™

Same string - Two derivations

num โ€˜+โ€™ num โ€˜*โ€™ num

num num

* num

num num

num +

Ambiguous Grammar

โ€ข Applying different derivation orders

produces different parse trees.

Leads to ambiguous results

Most probably will produce unexpected results.

โ€ข A CFG is ambiguous if the same string can

be associated with two distinct parse trees.

โ€“ E.g., The expression grammar can be shown to

be ambiguous using expressions containing

binary infix operators and non-fully

parenthesized expressions.

cs480(Prasad) L2Syntax 17

Removing Ambiguity

โ€ข Sometimes rewriting a grammar to reflect

operator precedence with additional

nonterminals will eliminate the ambiguity.

 * more binding than +.

 && has precedence over ||.

cs480(Prasad) L2Syntax 18

Eliminating Ambiguity

โ†’ โ€˜+โ€™

โ†’

โ†’ โ€˜*โ€™

โ†’

โ†’ num

โ†’ โ€˜(โ€™ โ€˜)โ€™

โ†’

โ†’ โ€˜(โ€™ โ€˜)โ€™

โ†’ num

โ†’ โ€˜+โ€™

โ†’ โ€˜*โ€™

Extended BNF

โ†’ (( โ€˜+โ€™ ) *) *

โ†’ (( โ€˜*โ€™ ) *) *

โ†’ num | โ€˜(โ€™ โ€˜)โ€™

โ†’ (( ) *) *

โ†’ โ€˜(โ€™ โ€˜)โ€™

โ†’ num

โ†’ โ€˜+โ€™ | โ€˜*โ€™

Expression Parser Fragment

Node expr() {

// PRE: Expects lookahead token.

// POST: Consume an Expression

// and update Lookahead token.

Node temp = term();

while ( inTok.ttype == '+') {

inTok.nextToken();

Node temp1 = term();

temp = new OpNode(temp,'+',temp1);

return temp;