This is an old revision of the document!
10. Writing a parser for a CF language
10.1. The grammar
10.1.1. Write a grammar which accurately describes regular expressions. Consider the following definition: A regular expression is built in the normal way, using the symbols (,),*,| and any other alpha-numeric caracter. Free spaces may occur freely within the expression.
10.1.2. Starting from the solution to the previous exercise, write an unambiguous grammar for regexes:
- Make sure to take precedence into account
10.2. A basic functional structure for a parser
Consider the following language encoding expressions:
- $ S \leftarrow M \mid M + S$
- $ M \leftarrow A \mid A * M$
- $ A \leftarrow 0 \mid 1 \mid (S)$
10.2.1. Implement an AST for expressions. 10.2.2. Implement a parser for expressions. Consider the following guidelines:
- A parser is a function which takes a string and has two tasks:
- returns the rest of the string to be parsed, or an error if parsing failed. Examples:
parse_whitespace(“ lfa”) = “lfa”
parse_whitespace(“lfa”) = None