Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions submission/paah/task4
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"os"
)

type Group struct {
Expression *Expression `'(' @@ ')'`
}

type Option struct {
Expression *Expression `'[' @@ ']'`
}

type Repetition struct {
Expression *Expression `'{' @@ '}'`
}

type Literal struct {
Start string `@String`
End string `[ '…' @String ]`
}

type Term struct {
Name string `@Ident |`
Literal *Literal `@@ |`
Group *Group `@@ |`
Option *Option `@@ |`
Repetition *Repetition `@@`
}

type Sequence struct {
Terms []*Term `@@ { @@ }`
}

type Expression struct {
Alternatives []*Sequence `@@ { '|' @@ }`
}

type Expressions []*Expression

type Production struct {
Name string `@Ident '='`
Expressions Expressions `@@ { @@ } '.'`
}

type EBNF struct {
Productions []*Production `{ @@ }`
}

func main() {
parser, err := participle.Build(&EBNF{}, nil)
if err != nil { panic(err) }

ebnf := &EBNF{}
err = parser.Parse(os.Stdin, ebnf)
if err != nil { panic(err) }

json.NewEncoder(os.Stdout).Encode(ebnf)
}