Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Operator Precedence Climbing #78

Open
cch123 opened this issue Feb 27, 2019 · 1 comment
Open

[Feature Request] Operator Precedence Climbing #78

cch123 opened this issue Feb 27, 2019 · 1 comment

Comments

@cch123
Copy link

cch123 commented Feb 27, 2019

{
//------ start
package main

type CompExpr struct {
    left string
    op string
    right string
}

type LogicExpr struct {
    left interface{}
    op string
    right interface{}
}

func main() {
    if len(os.Args) != 2 {
        log.Fatal("Usage: calculator 'EXPR'")
    }
    got, err := ParseReader("", strings.NewReader(os.Args[1]))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%#v\n", got)
}

// ------ end
}

Input <- expr:Expr EOF {
    return expr, nil
}

Expr <- LogicExpr / Atom

LogicExpr <- _ atom:Atom _ op: LogicOp _ expr: Expr _ {
    return  LogicExpr {left : atom, op : op.(string), right: expr}, nil
}

Atom <- '(' expr:Expr ')' {
    return expr, nil
} / _ field: Ident _ op:BinOp _ value:Value _{
    return CompExpr{left : field.(string), op: op.(string), right : value.(string)}, nil
}

LogicOp <- ("and" / "or"){
    return string(c.text), nil
}

BinOp <- ("!=" / ">=" / "<=" / "=" / "<>" / ">" / "<") {
    return string(c.text),nil
}

Ident <- [a-zA-Z][a-zA-Z0-9]* {
    return string(c.text),nil
}

Value <- [0-9]+ {
    return string(c.text),nil
}

_ "whitespace" <- [ \n\t\r]*

EOF <- !.

The right recursion grammar will auto-generate right association

If pigeon can implement precedence climbing or something else, it will be very convenient ~

@cch123 cch123 changed the title [Feature Request] Operator Precedence Define [Feature Request] Operator Precedence Climbing Feb 27, 2019
@breml
Copy link
Collaborator

breml commented Feb 28, 2019

This is not something that is planned to be implemented. See here for an example, how this can be achieved with the current implementation: https://github.com/mna/pigeon/blob/master/examples/calculator/calculator.peg

If you feel like implementing this, make an outline / design description first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants