SlideServe
Menu
Préférences
© SlideServe · v2026

Programming Languages

wesley-miles avatar
16 diapositives · 251 Views · Nov 13, 2014

Learn about context-free grammars, BNF shorthands, programming language syntax, parsing problems, regular grammars, and more in this comprehensive introduction to the grammatical structures of programming languages.

Programming Languages - slide 1
01 / 16Programming Languages
1 / 17

Télécharger la présentation

Programming Languages
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Programming Languages An Introduction to Grammars Spring 2004

  2. Context Free Grammars • One or more non terminal symbols • Lexically distinguished, e.g. upper case • Terminal symbols are actual characters in the language • Or they can be tokens in practice • One non-terminal is the distinguished start symbol.

  3. Grammar Rules • Non-terminal ::= sequence • Where sequence can be non-terminals or terminals • At least some rules must have ONLY terminals on the right side

  4. Example of Grammar • S ::= (S) • S ::= <S> • S ::= empty • This is the language D2, the language of two kinds of balanced parens • E.g. ((<<>>)) • Well not quite D2, since that should allow things like (())<>

  5. Example, continued • So add the rule • S ::= SS • And that is indeed D2 • But this is ambiguous • ()<>() can be parsed two ways • ()<> is an S and () is an S • () is an S and <>() is an S • Nothing wrong with ambiguous grammars

  6. BNF (Backus Naur/Normal Form) • Properly attributed to Sanskrit scholars • An extension of CFG with • Optional constructs in [] • Sequences {} = 0 or more • Alternation | • All these are just short hands

  7. BNF Shorthands • IF ::= if EXPR then STM [else STM] fi • IF ::= if EXPR then STM fi • IF ::= if EXPR then STM else STM fi • STM ::= IF | WHILE • STM ::= IF • STM ::= WHILE • STMSEQ ::= STM {;STM} • STMSEQ ::= STM • STMSEQ ::= STM ; STMSEQ

  8. Programming Language Syntax • Expressed as a CFG where the grammar is closely related to the semantics • For example • EXPR ::= PRIMARY {OP | PRIMARY} • OP ::= + | * • Not good, better is • EXPR ::= TERM | EXPR + TERM • TERM ::= PRIMARY | TERM * PRIMARY • This implies associativity and precedence

  9. PL Syntax Continued • No point in using BNF for tokens, since no semantics involved • ID ::= LETTER | LETTER ID • Is actively confusing since the BC of ABC is not an identifier, and anyway there is no tree structure here • Better to regard ID as a terminal symbol. In other words grammar is a grammar of tokens, not characters

  10. Grammars and Trees • A Grammar with a starting symbol naturally indicates a tree representation of the program • Non terminal on left is root of tree node • Right hand side are descendents • Leaves read left to right are the terminals that give the tokens of the program

  11. The Parsing Problem • Given a grammar of tokens • And a sequence of tokens • Construct the corresponding parse tree • Giving good error messages

  12. General Parsing • Not known to be easier than matrix multiplication • Cubic, or more properly n**2.71.. (whatever that unlikely constant is) • In practice almost always linear • In any case not a significant amount of time • Hardest part by far is to give good messages

  13. Regular Grammars • Also called type 3 • Only one non-terminal on right side • Right side is either • Terminal • Terminal Non-terminal • Correspond to regular expressions • Used to express grammar of tokens • Real_Literal ::= {digit}* . {digit}* [E[+|-]integer_literal]

  14. Context Sensitive Grammars • Also called type 1 • Left side can contain multiple symbols • JUNK digit => digit JUNK • But rhs cannot be shorter than left side • Can express powerful rules • But hard to parse

  15. Type 0 Grammars • No restrictions on left/right sides • Arbitrary mixture of terminals/non-terminals on either side • Very powerful (Turing powerful) • Impossible to parse 

  16. In Practice … • Context free grammars used to express syntax. • Other rules (formal english, VDL) used to express static semantics • Possible to do more in grammars • E.g. Algol-68 W-grammars • But too difficult to deal with