0

I'm writing a simple pascal parser and have specified some grammar rules like

program_header --> program, id, leftparenthesis ... etc
program --> [500]
id --> [300]

etc

How would I go about if I wanted to display diagnostic messages to the user? Like if someone left out "id", I would display that the next expected token would be "id" when it got "leftparenthesis" instead?

false
  • 10,264
  • 13
  • 101
  • 209
Stayfrosty555
  • 23
  • 1
  • 5

1 Answers1

0

A possibility could be

program_header -->
    [program], ( [id] ; {Err = miss_id} ), [lp], check_err(Err), [rp].

check_err(Err, Right, Right) :-
    var(Err) -> true ; length(Right, ToEnd), format('error ~s at ~d before end~n', [Err, ToEnd]).

test:

?- phrase(program_header,[program,lp,rp]).
error miss_id at 1 before end
true.

?- phrase(program_header,[program,id,lp,rp]).
true .

I've simplified the grammar, just to test...

CapelliC
  • 59,646
  • 5
  • 47
  • 90