From 3458de1e3a6e694c312c0e109b021a7206075152 Mon Sep 17 00:00:00 2001 From: Ayushman-Singh <37467936+Ayushman-Singh@users.noreply.github.com> Date: Sat, 17 Mar 2018 13:40:38 +0530 Subject: [PATCH] Create task4 --- submission/paah/task4 | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 submission/paah/task4 diff --git a/submission/paah/task4 b/submission/paah/task4 new file mode 100644 index 0000000..e3b27fe --- /dev/null +++ b/submission/paah/task4 @@ -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) +}