Skip to content

Commit

Permalink
[ BNF.cf ] Makefile to create LBNF parser. Comments and tidying BNF.cf.
Browse files Browse the repository at this point in the history
BNF.cf now systematically uses lists and coercions.
  • Loading branch information
andreasabel committed Jan 1, 2018
1 parent cac589c commit 13ec3d4
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 130 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ cabal.sandbox.config
/document/CC-2004.dvi
/document/CC-2004.ps
/document/LBNF-report.dvi
/source/src/LexBNF.hs
/source/src/LexBNF.info
/source/src/ParBNF.hs
/source/src/ParBNF.info
4 changes: 2 additions & 2 deletions source/src/AbsBNF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ data ProfItem = ProfIt [IntList] [Integer]
data IntList = Ints [Integer]
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Separation = SepNone | SepTerm String | SepSepar String
data Arg = Arg Ident
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Arg = Arg Ident
data Separation = SepNone | SepTerm String | SepSepar String
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Exp
Expand Down
125 changes: 62 additions & 63 deletions source/src/BNF.cf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}

-- A Grammar is a sequence of definitions
-- Multi-view grammars (undocumented)

LGr. LGrammar ::= [LDef] ;

Expand All @@ -26,37 +26,41 @@ DefSome. LDef ::= [Ident] ":" Def ;
LDefView. LDef ::= "views" [Ident] ;

separator LDef ";" ;
separator nonempty Ident "," ;

-- A Grammar is a sequence of definitions

Grammar . Grammar ::= [Def] ;

separator Def ";" ;

[] . [Item] ::= ;
(:) . [Item] ::= Item [Item] ;

--The rules of the grammar
Rule . Def ::= Label "." Cat "::=" [Item] ;
-- The rules of the grammar
Rule . Def ::= Label "." Cat "::=" [Item] ;

-- Items
Terminal . Item ::= String ;
NTerminal . Item ::= Cat ;

-- Categories
terminator Item "" ;

-- Categories (non-terminals)
ListCat . Cat ::= "[" Cat "]" ;
IdCat . Cat ::= Ident ;

-- labels with or without profiles
-- Labels with or without profiles
LabNoP . Label ::= LabelId ;
LabP . Label ::= LabelId [ProfItem] ;
-- A second label can be given in use of profiles and serves as
-- ``original function name'' (undocumented feature).
LabPF . Label ::= LabelId LabelId [ProfItem] ;
LabF . Label ::= LabelId LabelId ;

-- functional labels
Id . LabelId ::= Ident ;
Wild . LabelId ::= "_" ;
ListE . LabelId ::= "[" "]" ;
ListCons . LabelId ::= "(" ":" ")" ;
ListOne . LabelId ::= "(" ":" "[" "]" ")" ;
Id . LabelId ::= Ident ; -- AST constructor
Wild . LabelId ::= "_" ; -- No AST constructor (embedding)
ListE . LabelId ::= "[" "]" ; -- Empty list
ListCons . LabelId ::= "(" ":" ")" ; -- Cons
ListOne . LabelId ::= "(" ":" "[" "]" ")" ; -- Singleton list

-- profiles (= permutation and binding patterns)
ProfIt . ProfItem ::= "(" "[" [IntList] "]" "," "[" [Integer] "]" ")" ;
Expand All @@ -68,29 +72,33 @@ separator IntList "," ;
terminator nonempty ProfItem "" ;

-- Pragmas
Comment . Def ::= "comment" String ;
Comments . Def ::= "comment" String String ;
Internal . Def ::= "internal" Label "." Cat "::=" [Item] ;
Token. Def ::= "token" Ident Reg ;
PosToken. Def ::= "position" "token" Ident Reg ;
Entryp. Def ::= "entrypoints" [Ident] ;
Separator. Def ::= "separator" MinimumSize Cat String ;
Terminator. Def ::= "terminator" MinimumSize Cat String ;
Delimiters. Def ::= "delimiters" Cat String String Separation MinimumSize;
Coercions. Def ::= "coercions" Ident Integer ;
Rules. Def ::= "rules" Ident "::=" [RHS] ;
Function. Def ::= "define" Ident [Arg] "=" Exp ;

Comment . Def ::= "comment" String ; -- Line comment
Comments . Def ::= "comment" String String ; -- Block comment
Internal . Def ::= "internal" Label "." Cat "::=" [Item] ; -- No parsing, AST and printing only
Token. Def ::= "token" Ident Reg ; -- Lexer token
PosToken. Def ::= "position" "token" Ident Reg ; -- Lexer token with position info
Entryp. Def ::= "entrypoints" [Ident] ; -- Names of parsers
Separator. Def ::= "separator" MinimumSize Cat String ; -- List
Terminator. Def ::= "terminator" MinimumSize Cat String ; -- List
Delimiters. Def ::= "delimiters" Cat String String Separation MinimumSize;
Coercions. Def ::= "coercions" Ident Integer ; -- Embeddings and parenthesized exprs.
Rules. Def ::= "rules" Ident "::=" [RHS] ; -- Automatically generated lables (e.g. enums)
Function. Def ::= "define" Ident [Arg] "=" Exp ;

Arg. Arg ::= Ident ;
separator Arg "" ;

-- Lists
SepNone. Separation ::= ;
SepTerm. Separation ::= "terminator" String;
SepSepar. Separation ::= "separator" String;
SepSepar. Separation ::= "separator" String;

Layout. Def ::= "layout" [String] ;
LayoutStop. Def ::= "layout" "stop" [String] ;
LayoutTop. Def ::= "layout" "toplevel" ;
-- Layout
Layout. Def ::= "layout" [String] ; -- Layout start keywords
LayoutStop. Def ::= "layout" "stop" [String] ; -- Layout stop keywords
LayoutTop. Def ::= "layout" "toplevel" ; -- Should the toplevel be a block?

Arg. Arg ::= Ident ;
separator Arg "" ;
separator nonempty String "," ;

-- Expressions
Cons. Exp ::= Exp1 ":" Exp ;
Expand All @@ -102,50 +110,41 @@ LitString. Exp2 ::= String ;
LitDouble. Exp2 ::= Double ;
List. Exp2 ::= "[" [Exp] "]" ;

coercions Exp 2;

coercions Exp 2;
separator nonempty Exp2 "" ;
separator Exp "," ;

separator nonempty String "," ;
separator Exp "," ; -- list list
separator nonempty Exp2 "" ; -- argument list

RHS. RHS ::= [Item] ;
separator nonempty RHS "|" ;
RHS. RHS ::= [Item] ;

-- List size condition
MNonempty. MinimumSize ::= "nonempty" ;
MEmpty. MinimumSize ::= ;

-- regular expressions
RSeq. Reg2 ::= Reg2 Reg3 ; -- left-associative
RAlt. Reg1 ::= Reg1 "|" Reg2 ; -- left-associative
RMinus. Reg1 ::= Reg2 "-" Reg2 ; -- non-associative
-- Regular expressions
RSeq. Reg2 ::= Reg2 Reg3 ; -- left-associative
RAlt. Reg1 ::= Reg1 "|" Reg2 ; -- left-associative
RMinus. Reg1 ::= Reg2 "-" Reg2 ; -- non-associative

RStar. Reg3 ::= Reg3 "*" ;
RPlus. Reg3 ::= Reg3 "+" ;
ROpt. Reg3 ::= Reg3 "?" ;
RStar. Reg3 ::= Reg3 "*" ;
RPlus. Reg3 ::= Reg3 "+" ;
ROpt. Reg3 ::= Reg3 "?" ;

REps. Reg3 ::= "eps" ;
REps. Reg3 ::= "eps" ;

RChar. Reg3 ::= Char ; -- single character
RAlts. Reg3 ::= "[" String "]" ; -- list of alternative characters
RSeqs. Reg3 ::= "{" String "}" ; -- character sequence
RChar. Reg3 ::= Char ; -- single character
RAlts. Reg3 ::= "[" String "]" ; -- list of alternative characters
RSeqs. Reg3 ::= "{" String "}" ; -- character sequence

RDigit. Reg3 ::= "digit" ;
RDigit. Reg3 ::= "digit" ;
RLetter. Reg3 ::= "letter" ;
RUpper. Reg3 ::= "upper" ;
RLower. Reg3 ::= "lower" ;
RAny. Reg3 ::= "char" ;

_. Reg ::= Reg1 ;
_. Reg1 ::= Reg2 ;
_. Reg2 ::= Reg3 ;
_. Reg3 ::= "(" Reg ")" ;
RUpper. Reg3 ::= "upper" ;
RLower. Reg3 ::= "lower" ;
RAny. Reg3 ::= "char" ;

-- list of categories in the entrypoint pragma
(:[]). [Ident] ::= Ident ;
(:). [Ident] ::= Ident "," [Ident] ;
coercions Reg 3;

-- comments in BNF source
comment "--" ;
-- Comments in BNF source
comment "--" ;
comment "{-" "-}" ;
8 changes: 4 additions & 4 deletions source/src/BNFC/GetCF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ checkRule cf (Rule (f,_) cat rhs)
-- let Ok tree = expandRules (Abs.Grammar [rules1])
-- in putStrLn (printTree tree)
-- :}
-- Foo_abc . Foo ::= "abc" ;
-- FooA . Foo ::= A ;
-- Foo1 . Foo ::= "foo" "bar" ;
-- Foo_abc . Foo ::= "abc";
-- FooA . Foo ::= A;
-- Foo1 . Foo ::= "foo" "bar";
-- Foo2 . Foo ::= "++"
--
-- Note that if there are two `rules` macro with the same category, the
Expand All @@ -452,7 +452,7 @@ checkRule cf (Rule (f,_) cat rhs)
-- let Ok tree = expandRules (Abs.Grammar [rules1, rules2])
-- in putStrLn (printTree tree)
-- :}
-- Foo1 . Foo ::= "foo" "bar" ;
-- Foo1 . Foo ::= "foo" "bar";
-- Foo2 . Foo ::= "foo" "foo"
--
-- This is using a State monad to remember the last used index for a category.
Expand Down
1 change: 0 additions & 1 deletion source/src/ErrM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ instance Applicative Err where
(Bad s) <*> _ = Bad s
(Ok f) <*> o = liftM f o


instance Functor Err where
fmap = liftM

Expand Down
2 changes: 1 addition & 1 deletion source/src/LexBNF.x
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $i = [$l $d _ '] -- identifier character
$u = [\0-\255] -- universal: any character

@rsyms = -- symbols and non-identifier-like reserved words
\: | \; | \. | \: \: \= | \[ | \] | \_ | \( | \) | \, | \= | \| | \- | \* | \+ | \? | \{ | \}
\: | \; | \, | \. | \: \: \= | \[ | \] | \_ | \( | \) | \= | \| | \- | \* | \+ | \? | \{ | \}

:-
"--" [.]* ; -- Toss single line comments
Expand Down
18 changes: 18 additions & 0 deletions source/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Makefile for bnfc's own parser
# Author: Andreas Abel, 2018-01-01

# BNFC-generated files
gen = AbsBNF.hs DocBNF.txt ErrM.hs LexBNF.x ParBNF.y PrintBNF.hs SkelBNF.hs TestBNF.hs

all : AbsBNF.hs LexBNF.hs ParBNF.hs

Abs%.hs Doc%.txt Lex%.x Par%.y Print%.hs Skel%.hs Test%.hs : %.cf
bnfc --haskell --ghc $<

%.hs : %.x
alex $<

%.hs : %.y
happy -i $<

#EOF
Loading

0 comments on commit 13ec3d4

Please sign in to comment.