Context Free Grammars
Describes sets of strings, in particular context free languages Abstracted: convert from one language to another Three things we need for a translator:
- Lexer
- Typically uses RE to convert text file into a list of tokens (word types)
- Parser
- Converts tokens to an Abstract Syntax Tree using a Context Free Grammar
- Evaluator/Generator
- Converts AST to Target or evaluates to value
CFG
Another example - recursively defined set
Terms
Nonterminal symbol - a symbol that is not terminated (done): denoted by convention as a capital letter - describes a set, Terminal symbol - lowercase, describe symbols that are allowed in the language Production rule - rules about replacement
In :
- , , are terminals
- is a nonterminal
- , , are production rules
Derivation - a proof of a string being in using just the production rules
Another CFG
aSa bSb cSc a b c e - palindrome
Expressions
Let’s take the grammar
You could say something like .
Then
But for 1+2+3
This enforces a certain shape of a tree. The left hand side MUST be a number or a multiplication - only the right hand side can be another plus.