parsing - How to resolve ambiguity in the definition of an LR(1) grammar? -


i writing golang compiler in ocaml, , argument lists causing me bit of headache. in go, can group consecutive parameter names of same type in following way:

func f(a, b, c int)  ===  func f(a int, b int, c int) 

you can have list of types, without parameter names:

func g(int, string, int) 

the 2 styles cannot mix-and-matched; either parameters named or none are.

my issue when parser sees comma, doesn't know do. in first example, a name of type or name of variable more variables coming up? comma has dual role , not sure how fix this.

i using menhir parser generator tool ocaml.

edit: @ moment, menhir grammar follows rules specified @ http://golang.org/ref/spec#function_types

as written, go grammar not lalr(1). in fact, not lr(k) k. is, however, unambiguous, parse glr parser, if can find 1 (i'm pretty sure there several glr parser generators ocaml, don't know enough of them recommend one).

if don't want (or can't) use glr parser, can same way russ cox did in gccgo compiler, uses bison. (bison can generate glr parsers, cox doesn't use feature.) technique not rely on scanner distinguishing between type-names , non-type-names.

rather, accepts parameter lists elements either name_or_type or name name_or_type (actually, there more possibilities that, because of ... syntax, doesn't change general principle.) that's simple, unambiguous , lalr(1), overly-accepting -- accept func foo(a, b int, c), example -- , not produce correct abstract syntax tree because doesn't attach type list of parameters being declared.

what means once argument list parsed , inserted ast attached function declaration (for example), semantic scan performed fix and, if necessary, produce error message. scan done right-to-left on list of declaration elements, specified type can propagated left.

it's worth noting grammar in reference manual overly-accepting, because not express constraint "either parameters named or none are". constraint could expressed in lr(1) grammar -- i'll leave exercise readers -- resulting grammar lot more difficult understand.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -