Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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: * $math[S \leftarrow M \mid M + S] * $math[M \leftarrow A \mid A * M] * $math[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''