Flex-compatible lexer generator for Go
This repository has been archived on 2024-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Amelia Cuss eb0e018a9d README: link to canonical source. 2024-01-18 17:38:14 +11:00
.gitignore revise 2022-12-20 22:22:45 +11:00
README.md README: link to canonical source. 2024-01-18 17:38:14 +11:00
abcd.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00
buffers.l further test cases from flex manual 2011-04-25 11:40:58 +10:00
comment.l further test cases from flex manual 2011-04-25 11:40:58 +10:00
counter.l our versions of flex manual examples, allowing definitions in prologue 2011-04-25 11:02:30 +10:00
cstyle.l add support for regexp count ranges, e.g. (xyz){1,3} 2011-04-25 17:31:58 +10:00
expect.l further test cases from flex manual 2011-04-25 11:40:58 +10:00
foobar.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00
go.mod revise 2022-12-20 22:22:45 +11:00
golex.go Some fixes and gofmt for Go weekly, incl. simplifications now that we have a real RE library. 2011-12-24 14:23:29 +11:00
input.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00
lexfile.go Fixes for Go 1. 2012-04-02 17:32:51 +10:00
megakludge.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00
nilmatch.l Add experimental support for detecting whether or not a rule could possibly match the empty string (an error). 2011-04-28 20:31:09 +10:00
parser.go Force writing out the last rule if we hadn't done so by EOF. Fixes #18. 2011-12-31 09:36:25 +11:00
regexp.go Restore broken (!) substitution functionality. 2012-04-02 17:32:56 +10:00
test revise 2022-12-20 22:22:45 +11:00
toypascal.l our versions of flex manual examples, allowing definitions in prologue 2011-04-25 11:02:30 +10:00
unput.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00
username.l our versions of flex manual examples, allowing definitions in prologue 2011-04-25 11:02:30 +10:00
wordcount.l test cases as found in flex manual 2011-04-25 11:23:03 +10:00

golex

flex-compatible lexical analyser generator

introduction

golex is a flex-compatible lexical analyser generator, written for Go 1.

The below description has been pilfered from flex's description in Debian, adapted to describe golex:

golex is a tool for generating scanners: programs which recognize lexical patterns in text. It reads the given input files for a description of a scanner to generate. The description is in the form of pairs of regular expressions and Go code, called rules. golex generates as output a Go source file, which defines a routine yylex(). When the routine is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding Go code.

notes

golex supports all features for regular expression matching as described in flex's manual, except:

  • character class set operations [a-z]{-}[aeiou], and
  • matching EOF <<EOF>>.

EOF-matching is intended to be added to a future release of golex. Character class operations, however, will not, unless Go's own regular expression library (based on RE2) comes to.

A number of utility functions required for full flex emulation (mostly concerning manipulating the buffer (stack)) are also not yet available.

The full set of omissions (in regular expressions and otherwise) is detailed in the GitHub Issues for this repository.

golex and the scanners it generates are not fast (unlike those of flex). Rather than implementing its own regular expression engine and crafting a state machine based on that, golex simply defers to Go's built-in regular expressions, and matches character-by-character. Pull requests to right this wrong gratefully accepted! :)

examples

Self-contained examples, taken from throughout the flex manual, have been converted to Go and are included as *.l in this distribution. I invite you to compare them to the original flex examples to note how similar they are. Here are a few examples, found as username.l, counter.l, and toypascal.l in the golex distribution.

A test script for building and running an example is included. For example:

./test toypascal.l

will build golex, run golex on toypascal.l, build the resulting Go code, and then run the resulting lexer.

other golexen

This is not the first attempt at writing a golex utility, though it might be the first with the aim of behaving as similarly to the original flex as possible.

Other golexen include (but are not limited to):

  • Ben Lynn's Nex tool.
  • CZ.NIC's package at git://git.nic.cz/go/lex.
  • CZ.NIC's tool at git://git.nic.cz/go/golex (it's not like the name is terribly original!).

license

Copyright 2011-2022 Asherah Connor. Licensed under the 2-Clause BSD License.