Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
lfa:lab10-lexer [2021/12/14 16:22]
pdmatei
lfa:lab10-lexer [2021/12/14 16:31] (current)
pdmatei
Line 10: Line 10:
 10.1.1. Implement an AST for expressions. 10.1.1. Implement an AST for expressions.
  
-  * A **parser** is a function which takes a string and has two tasks: ​+  * 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:       - returns the **rest of the string to be parsed**, or an error if parsing failed. Examples:
           * ''​parse_whitespace("​ lfa") = "​lfa"''​           * ''​parse_whitespace("​ lfa") = "​lfa"''​
           * ''​parse_whitespace("​lfa"​) = None''​           * ''​parse_whitespace("​lfa"​) = None''​
       - adds the parsed value to **a global stack** whenever the value is part of the AST to be built.       - adds the parsed value to **a global stack** whenever the value is part of the AST to be built.
-  * We can build **more complex parsers** from simpler ones. The key is to **try** to parse expressions and if parsing fails, we can try a different alternative. 
  
-Consider the framework shown below:+Another example:
 <code python> <code python>
 stack = []  #  stack = []  # 
Line 34: Line 33:
 10.1.3. Implement a function ''​parse_plus''​ which parses the character '​+'​ (if the first character is '​+',​ it consumes it, otherwise it fails). Hint: use a more general function which you can then reuse to parse other characters. 10.1.3. Implement a function ''​parse_plus''​ which parses the character '​+'​ (if the first character is '​+',​ it consumes it, otherwise it fails). Hint: use a more general function which you can then reuse to parse other characters.
  
-10.1.4. Complete the following implementation of the function ''​parse_multiplication'':​+10.1.4. ​We can build **more complex parsers** from simpler ones. The key is to **try** to parse expressions and if parsing fails, we can try a different alternative. 
 +Complete the following implementation of the function ''​parse_multiplication'':​
  
 <code python> <code python>
Line 47: Line 47:
         w2 = parse_plus(w1)         w2 = parse_plus(w1)
         if w2 != None:         if w2 != None:
-            # we have parsed a '​+'​+            # we have successfully ​parsed a '​+'​
             w3 = parse_multiplication(w2)             w3 = parse_multiplication(w2)
             if w3 != None:             if w3 != None:
Line 53: Line 53:
                 # what are the contents of the stack right now?                 # what are the contents of the stack right now?
                 # how should the stack be modified?                 # how should the stack be modified?
 +        else:
 +            # parsing a '​+'​ has failed, so we just return the rest of the string w1
 +            return w1
     else:      else: 
        ​return None # parsing a digit failed        ​return None # parsing a digit failed