230 likes | 420 Views
ITEC 380. Organization of programming languages Lecture 2 – Grammar / Language capabilities. Review. Definition / purpose of a language Paradigms Compilation / Interpretation Stages of compilation. Objectives. Grammars Rules of languages. Compiling.
E N D
ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities
Review • Definition / purpose of a language • Paradigms • Compilation / Interpretation • Stages of compilation
Objectives • Grammars • Rules of languages
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
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
Are the following sentences valid? I saw a dog chasing the ball. The dog chased the cat the dog chased the cat 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>
Implementation • How would we implement a parser for the previous language? • Thoughts / ideas?
One way main parseLines() Input -getToken() parseLines nounPhrase verb article noun Recursive decent parser is probably the easiest way to implement
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
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
EBNF • Slightly more rule happy • Mainly geared to make it easier to write • Rules • [] means optional • {} means 0 or more repetitions • (|) means alternatives
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
Details What is interesting about the grammar S --> aS | a or S ::= aS| a • 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
Other languages • L --> aLbLc | ab | bc • Whatareexamplesentencesgivenbythislanguage? • 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
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
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?
Simple language How would we parse if .. then if .. then .. else .. • <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>
Trees • What do you remember from your earlier programming courses about tree data structures?
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
Abstract / Concrete • Two methods for handling information • Keep track of non-terminals • Remove non-terminals • Terminals become leaf nodes • Operations become interior nodes
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
Review • Grammars • BNF/EBNF • Parse trees • How to define / recognize a language • Specifics • Compilers
Next week • Functional programming - Lisp