Haskell: syntax error when adding new line to pattern matching

I basically modify the parser to handle additional statements. Before my changes, one part of the analyzer looked like this:

parseExpRec e1 (op : ts) = let (e2, ts') = parsePrimExp ts in case op of T_Plus -> parseExpRec (BinOpApp Plus e1 e2) ts' T_Minus -> parseExpRec (BinOpApp Minus e1 e2) ts' T_Times -> parseExpRec (BinOpApp Times e1 e2) ts' T_Divide -> parseExpRec (BinOpApp Divide e1 e2) ts' _ -> (e1, op : ts) 

T_Plus, etc. are members of the data type Token, and Plus, Minus, etc. are part of BinOp, which BinOpApp applies to two operands. I updated the Token and BinOpApp data types to handle Power token (exponentiation). This is the resulting code:

 parseExpRec e1 (op : ts) = let (e2, ts') = parsePrimExp ts in case op of T_Plus -> parseExpRec (BinOpApp Plus e1 e2) ts' T_Minus -> parseExpRec (BinOpApp Minus e1 e2) ts' T_Times -> parseExpRec (BinOpApp Times e1 e2) ts' T_Divide -> parseExpRec (BinOpApp Divide e1 e2) ts' T_Power -> parseExpRec (BinOpApp Power e1 e2) ts' _ -> (e1, op : ts) 

It seems simple, but now it gives the following error:

TXL.hs: 182: 13: parsing error at input '->'

Line 182 is the line in which I added "T_Power β†’ parseExpRec ..." - I don’t see how it differs from other lines that are perfectly parsed. I use GHCi as a medium.

+4
source share
3 answers

Have you separated a new line with the same space delimiters as the previous ones? Or is there a tab character there?

+6
source

This, with almost 100% certainty, is an indent error. I had similar problems in the past, also when writing a parser. These are probably the lines before the problematic line is indented with tabs and you used spaces in the T_Power line (or something similar). Can you include printable characters in your editor?

+2
source

Perhaps you still need to configure T_Power in the lexer? For example, what character do you use to exponentiate (e.g. ^), and where does this relate to T_Power?

I don't know if this one works (or something like that), but it could be something like:

 scanner ('^' : cs) = T_Power : scanner cs 
0
source

Source: https://habr.com/ru/post/1302229/


All Articles