List Info

Thread: Re: "ocaml_beginners"::[] printing line numbers in ocamlyacc generated parsers




Re: "ocaml_beginners"::[] printing line numbers in ocamlyacc generated parsers
country flaguser name
United States
2007-07-25 09:20:53

> Just catch the parser error exception and print the current line
number from
> its mutable variable.

I'm obviously missing something. The exception Parser_error does not
carry a value, so what is "its mutable variable"? The lexbuf is only
in scope within semantic actions.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] printing line numbers in ocamlyacc generated parsers
country flaguser name
France
2007-07-25 09:50:31

Ashish Agarwal a écrit :
>> Just catch the parser error exception and print the current line
>; number from
>>; its mutable variable.
>
> I'm obviously missing something. The exception Parser_error does not
> carry a value, so what is "its mutable variable"? The lexbuf is only
>; in scope within semantic actions.

I did this the following way:

let buf = Lexing.from_channel cin in
try
let v = Parser.main Lexer.token buf in
...
with
| Lexer.Error msg ->
Format.fprintf err "Lexer error: %s!." msg
| Parser.Error ->
let p = buf.Lexing.lex_curr_p in
let l = p.Lexing.pos_lnum in
let c = p.Lexing.pos_cnum - p.Lexing.pos_bol in
Format.fprintf err "Parser error at line %d char %d." l c

--
Johannes Kanig
johannes.kanig%40via.ecp.fr">johannes.kanigvia.ecp.fr

__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Ads on Yahoo!

Learn more now.

Reach customers

searching for you.

Yoga Groups

Exchange insights

with members of

the yoga community.

Re: "ocaml_beginners"::[] printing line numbers in ocamlyacc generated parsers
country flaguser name
United Kingdom
2007-07-25 12:28:07

On Wednesday 25 July 2007 15:20:53 Ashish Agarwal wrote:
> > Just catch the parser error exception and print the current line
>;
> number from
>;
> > its mutable variable.
>
> I'm obviously missing something. The exception Parser_error does not
> carry a value, so what is "its mutable variable"? The lexbuf is only
>; in scope within semantic actions.

Yes. The easiest way is to stick a:

let line = ref 0

in the parser's header (that's what I meant by "the current number's mutable
variable") and add "incr Parser.line" in the lexer when it sees 'n'. Then
catch the parse error and do:

| Parsing.Parse_error ->
printf "Parse error at line %dn" !line

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )