H. Conrad Cunningham
24 November 2018
Copyright (C) 2017, 2018, H. Conrad Cunningham
Acknowledgements: I originally created these slides in Fall 2017 to accompany what is now Chapter 41, Calculator: Concrete Syntax, in the 2018 version of the textbook Exploring Languages with Interpreters and Functional Programming.
Browser Advisory: The HTML version of this document may require use of a browser that supports the display of MathML. A good choice as of November 2018 is a recent version of Firefox from Mozilla.
Overview concepts of concrete syntax — grammars, derivations
Examine concrete syntax for simple ELI Calculator language
Discussion in ELIFP textbook on Grammars
Familiar from CSci 311 Models of Computation
Only informal understanding required
Concrete syntax: express language details in text strings (source code) — this chapter
Abstract syntax: express essential structure, ignore incidental representation issues (intermediate representation) — next chapter
Examples — familiar syntax of arithmetic expressions
3
-3
x
1+1
x + 3
(x + y) * (2 + z)
-- Context-free grammar
<expression> ::= <term> { <addop> <term> }
<term> ::= <factor> { <mulop> <factor> }
<factor> ::= <var> | <val>
| '(' <expression> ')'
<val> ::= [ '-' ] <unsigned>
<var> ::= <id>
<addop> ::= '+' | '-'
<mulop> ::= '*' | '/'
-- Regular grammar
<id> ::= <firstid> | <firstid> <idseq>
<idseq> ::= <restid> | <restid> <idseq>
<firstid> ::= <alpha> | '_'
<restid> ::= <alpha> | '_' | <digit>
<unsigned> ::= <digit> | <digit> <unsigned>
<digit> ::= any numeric character
<alpha> ::= any alphabetic character
1 + 1
Examples — parenthesized syntax (Lisp-like)
3
3
x
(+ 1 1)
(+ x 3)
(* (+ x y) (+ 2 z))
-- Context-free grammar
<expression> ::= <var> | <val> | <operexpr>
<var> ::= <id>
<val> ::= [ "-" ] <unsigned>
<operexpr> ::= '(' <operator> <operandseq> ')'
<operandseq> ::= { <expression> }
<operator> ::= '+' | '*' | '-' | '/' | ...
-- Regular grammar (same as infix grammar)
<id> ::= <firstid> | <firstid> <idseq>
<idseq> ::= <restid> | <restid> <idseq>
<firstid> ::= <alpha> | '_'
<restid> ::= <alpha> | '_' | <digit>
<unsigned> ::= <digit> | <digit> <unsigned>
<digit> ::= any numeric character
<alpha> ::= any alphabetic character
(+ 1 1)
Grammars — regular and context-free, derivations, derivation (parse) tree
Concrete syntax — ELI Calculator language