@@ -3,7 +3,8 @@
33Parser
44======
55
6−To run our parser we call 'parse'. This requires a Grammar and the output from the lexer (a list of tokens).
6+To run our parser we call 'parse'. This requires a Grammar and the output
7+from the lexer (a list of tokens).
78
89.. code-block:: idris
910
@@ -18,8 +19,9 @@ If successful this returns 'Right' with a pair of
1819otherwise it returns 'Left' with the error message.
1920
2021So we need to define the Grammar for our parser, this is done using the following
21−'Grammar' data structure. This is a combinator structure, similar in principle to the
22−recogniser combinator for the lexer, which was discussed on the previous page.
22+'Grammar' data structure. This is a combinator structure, similar in principle
23+to the recogniser combinator for the lexer, which was discussed on the
24+previous page.
2325
2426As with the Recogniser the Grammar type is dependent on a boolean 'consumes'
2527value which allows us to ensure that complicated Grammar structures will always
@@ -111,8 +113,8 @@ parse which takes two parameters:
111113 (Integer, List (TokenData ExpressionToken))
112114 test1 s = parse intLiteral (fst (lex expressionTokens s))
113115
114−As required, if we pass it a string which is a number literal then it will return the
115−number in the 'Right' option.
116+As required, if we pass it a string which is a number literal then it will
117+return the number in the 'Right' option.
116118
117119.. code-block:: idris
118120
@@ -260,7 +262,8 @@ token lists are not:
260262
261263.. list-table::
262264
263− * - Now we can generate a Grammar for an expression inside parenthesis like this.
265+ * - Now we can generate a Grammar for an expression inside parenthesis
266+ like this.
264267
265268 - .. code-block:: idris
266269
@@ -281,7 +284,8 @@ value from the inner expression.
281284 expr = (add expr (op "+") expr)
282285
283286This is a potentially infinite structure which is not total.
284−In order to work up to this gradually I will start with prefix operators (sometimes known as Polish notation) then modify later for infix operators.
287+In order to work up to this gradually I will start with prefix operators
288+(sometimes known as Polish notation) then modify later for infix operators.
285289
286290.. list-table::
287291
@@ -490,16 +494,20 @@ If we have a rule like this:
490494
491495 A -> (x<*>y) <|> (x<*>z)
492496
493−If 'x<*>y' fails but 'x<*>z' would succeed a problem is that, 'x<*>y' has already consumed 'x', so now 'x<*>z' will fail.
497+If 'x<*>y' fails but 'x<*>z' would succeed a problem is that, 'x<*>y' has
498+already consumed 'x', so now 'x<*>z' will fail.
494499
495−so we could write code to backtrack. That is 'try' 'x<*>y' without consuming so that, if the first token succeeds but the following tokens fail, then the first tokens would not be consumed.
500+so we could write code to backtrack. That is 'try' 'x<*>y' without consuming
501+so that, if the first token succeeds but the following tokens fail, then the
502+first tokens would not be consumed.
496503
497504Another approach is left factoring:
498505
499506Left Factoring
500507--------------
501508
502−Replace the rule with two rules (that is we add a non-terminal symbol) so for example, instead of:
509+Replace the rule with two rules (that is we add a non-terminal symbol) so
510+for example, instead of:
503511
504512.. code-block:: idris
505513
@@ -512,9 +520,11 @@ we add an extra rule to give:
512520 A -> x<*>N
513521 N -> y <|> z
514522
515−That is we convert a general context-free grammar to a LL(1) grammar. Although not every context-free grammar can be converted to a LL(1) grammar.
523+That is we convert a general context-free grammar to a LL(1) grammar. Although
524+not every context-free grammar can be converted to a LL(1) grammar.
516525
517−This still does not solve the infinite recursion issue and there is another problem: the precedence of the operators +, - and * is not explicit.
526+This still does not solve the infinite recursion issue and there is another
527+problem: the precedence of the operators +, - and * is not explicit.
518528
519529To resolve this we can alter the example like this:
520530