itec 380 organization of programming languages lecture 2 – grammar / language capabilities

23
ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities

Upload: alan-anthony

Post on 27-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

ITEC 380

Organization of programming languages

Lecture 2 – Grammar / Language capabilities

Grammar

Review

• Definition / purpose of a language• Paradigms• Compilation / Interpretation• Stages of compilation

Grammar

Objectives

• Grammars• Rules of languages

Grammar

Compiling

• You know the basic stages of what goes on when a program is compiled

• Details details– How to recognize a sentence in a

language

• Rules for recognizing a language

Grammar

Language

• Made up of any possible sentences made using a grammar

• All sentences made by a grammar have to be part of the language

• Grammars provide rules for what is possible

Grammar

Example grammar

• Nouns: either cat or dog– Represented by <noun> ::= cat|dog

• Verbs– <verb> ::= saw | chased

• Articles: – <article> ::= a | the

• Noun phrases: – <nounPhase> ::= <article> <noun>

• Sentences: – <sentence> ::= <nounPhase> <verb>

<nounPhase>

Are the following sentences valid?I saw a dog chasing the ball.The dog chased the catthe dog chased the cat

Grammar

Implementation

• How would we implement a parser for the previous language?

• Thoughts / ideas?

Grammar

One way

Input-getToken()

mainparseLines()

parseLines

nounPhrase

article noun

verb

Recursive decent parser is probably the easiest way toimplement

Grammar

Issues

• Not an easy task• Grammars are quite large for well

known languages– http://docs.oracle.com/javase/specs/jls/

se7/html/jls-18.html

• Benefits– Implementation of a language– Tools that provide additional features

Grammar

BNF

• Backus Naur Form• Each rule has one non-terminal at

the left hand side• Makes implementation easier than– Noun|Verb ::= cat dog catch

• Does not solve all issues though– Language features / how the grammar is

parsed

Grammar

EBNF

• Slightly more rule happy• Mainly geared to make it easier to

write• Rules– [] means optional– {} means 0 or more repetitions– (|) means alternatives

Grammar

Examples

• BNF:– E -> E + T | E - T | T – T -> T * F | T / F | F – F -> a | b | c

• EBNF: – E -> T { (+ | -) T } – T -> F { (* | /) F } – F -> a | b | c

Grammar

Details

• Terminals– Keywords

• Variables– Non-Terminals

• Substitution Rules– Drill down

• Start Symbol– Where does it all begin?

• Litmus test– Grammar must provide all possible sentences in a language– All sentences produced by a grammar must be in a

language

What is interesting about the grammarS --> aS | a or S ::= aS | a

Grammar

Other languages

• L --> aLbLc | ab | bc• What are example sentences given by

this language?• S --> aSBA • S --> abA • AB --> BA • bB --> bb • bA --> ba • aA --> aa

Is this a BNF?What are some issues with this language?Context sensitivity

Grammar

Ambiguity

• Example language– A --> aA | Aa | a

• What are some issues with this langauge that might come up with the sentence aa?

• Other issues– Precedence i.e. and before or

Grammar

Numbers

• Example• <integer> --> <digit><integer> | <digit> • <digit> --> 0 | 1 | 2 | 3 ... | 9 • <ident> --> <letter><letter><letterOrDigitSeq> • <letterOrDigitSeq> --> <letterOrDigit>|

<letterOrDigit><letterOrDigitSeq> <letterOrDigit> --> <letter> | <digit>

• <letter> --> a | b | c ...

What would we add to handle floating point numbers?What would be the starting point for this grammar?What changes would need to be made for implementation?

Grammar

Simple language

• <program> --> program <ident> is <stmtList> end; • <stmtList> --> <stmt> | <stmt> <stmtList>• <stmt> --> <ident> := <expr> ; | if <boolExp> then <stmtList> end if ; | if <boolExp> then <stmtList> else <stmtList> end if ; | begin <stmt> end ;|

... • <expr> --> <expr> + <expr> | <ident> | <integer> | ( <expr> ) |

...<boolExp> --> true|false<ident> --> <letter> | <letter><letterOrDigit><ident>|

<letter><letterOrDigit>

How would we parseif .. then if .. then .. else ..

Grammar

Trees

• What do you remember from your earlier programming courses about tree data structures?

Grammar

Methods

• Use recursive functions to read / handle code generation

• Use recursive functions to build up a tree data structure

• Prune data structure using optimization

• Use it to generate code by going either bottom to top (left to right or right to left), or top down

Grammar

Abstract / Concrete

• Two methods for handling information

• Keep track of non-terminals• Remove non-terminals– Terminals become leaf nodes– Operations become interior nodes

Grammar

Types

• LL– Top down context free grammar– Goes from left to right– Specify number of lookahead characters– LL(1) – Easy to make

• LR– Bottom up non-ambiguous context free

grammar– Linear time– Often generated by tools

Grammar

Review

• Grammars• BNF/EBNF• Parse trees• How to define / recognize a language• Specifics– Compilers

Grammar

Next week

• Functional programming - Lisp