views:

91

answers:

3

Basically I'm modifying a parser to handle additional operators. Before my changes, one part of the parser 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 Token datatype, and Plus, Minus etc. are part of BinOp which BinOpApp applies to two operands. I have updated the Token and BinOpApp datatypes to handle the Power (exponentiation) token. 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)

This seems simple but it's now giving the following error:

TXL.hs:182:13: parse error on input '->'

Line 182 is the line where I added "T_Power -> parseExpRec..." - I don't see how it's any different from the other lines, which parse fine. I'm using GHCi as my environment.

A: 

Maybe you still need to setup T_Power in the lexer? For example, what symbol are you using for exponentiation (e.g. ^), and where is that associated with T_Power?

I don't know if this (or something similar) is what you're working from, but maybe something like:

scanner (’^’ : cs) = T_Power : scanner cs
datageist
+6  A: 

Have you indented the new line with the same space-delimiters as the previous ones? Or, has a tab character snuck in there?

jmtd
+2  A: 

This is almost with 100% certainty an indentation error. I've had similar problems in the past, also when writing a parser. What's probably happened is the lines before the problematic line are indented with tabs, and you've used spaces on the T_Power line (or something similar). Can you turn on non-printed characters in your editor?