Giter VIP home page Giter VIP logo

go-lexer-parser's Introduction

Go play with parser combinators...

This repo is a simple parser combinator implementation in Go.

In parse.go you will find the real brains of the repo:

Is, Wildcard, Or, And, Many, Optional, OneOrMore

These may be functionally composed to parse more interesting things. To aid in this process, I used the combinators to create a shorthand for writing parsers. The shorthand may be found in shorthand.go, and is defined as follows:

literal -> ' & [*] & '
reference -> character [character]
many -> [ & expression & ]
and -> "&"
or -> "|"
wildcard -> "* & literal"
optional -> ( & expression & )
component -> literal
		   | expression
		   | reference
		   | many
		   | optional
		   | { & expression & }
		   | wildcard
expression -> component [ or component ]
		   |  component [ and component ]

This is slightly easier to understand by example. The following describes a parser that can be used to parse math expressions.

var math = parse.Grammar{
	"digit":    "'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'",
	"sign":     ` '+'|'-' `,
	"operator": " '*'|'/'|'+'|'-'|'^' ",
	"digits":   "digit & [digit] ",
	"number": ` { digits & 'e' & digits } | digits 
			  | { '(' & (sign) & digits & ')' }`,
	"component":  "number | { '(' & expression & ')' }",
	"expression": "component & [{operator & component}]",
}

This may be 'compiled' and used as follows:

input := '1+(1+1)'
// create a lexer for controlling the flow of characters
lexer := parse.NewLexer(input)
// create the parser by parsing the shorthand
parser := math.GetParser("expression")
// parse the input, return a concrete syntax tree & a boolean 'matches'
matches, cst := parser(lex)

The concrete syntax tree can be further processed to do something useful, such as evaluating the expression.

Run the examples with:

$ go run main.go

Hats off to The Orange Duck for inspiration

go-lexer-parser's People

Contributors

kctess5 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.