Shortened PEG grammar of Lojban

From Lojban
Revision as of 10:32, 20 October 2014 by Gleki (talk | contribs) (Gleki moved page Shortened PEG grammar to Shortened PEG grammar of Lojban without leaving a redirect)
Jump to: navigation, search
; This is a Parsing Expression Grammar for Lojban.
; See http://www.pdos.lcs.mit.edu/~baford/packrat/
; 
; All rules have the form:
; 
; 	name <- peg-expression
; 
; which means that the grammatical construct "name" is parsed using
; "peg-expression".  
; 
; 1)  Names in lower case are grammatical constructs.
; 2)  Names in UPPER CASE are selma'o (lexeme) names, and are terminals.
; 3)  Concatenation is expressed by juxtaposition with no operator symbol.
; 4)  / represents *ORDERED* alternation (choice).  If the first
;     option succeeds, the others will never be checked.
; 5)  ? indicates that the element to the left is optional.
; 6)  * represents optional repetition of the construct to the left.
; 7)  + represents one-or-more repetition of the construct to the left.
; 8)  () serves to indicate the grouping of the other operators.
;
; Longest match wins.

; --- GRAMMAR ---


PUBLIC text <- intro-null NAI-clause* text-part-2 (!text-1 joik-jek)? text-1? faho-clause EOF?

text-part-2 <- (CMENE-clause+ / indicators?)

; Please note that the "text-1" item in the text-1 production does
; *not* match the BNF. This is due to a bug in the BNF.  The change
; here was made to match grammar.300
text-1 <- I-clause (jek / joik)? (stag? BO-clause)? text-1? / NIhO-clause+ paragraphs? / paragraphs

paragraphs <- paragraph (NIhO-clause+ paragraphs)?

paragraph <- (statement / fragment) (I-clause !jek !joik !joik-jek (statement / fragment)?)*

statement <- statement-1 / prenex statement

statement-1 <- statement-2 (I-clause joik-jek statement-2?)*

statement-2 <- statement-3 (I-clause (jek / joik)? stag? BO-clause statement-2)? / statement-3 (I-clause (jek / joik)? stag? BO-clause)?

statement-3 <- sentence / tag? TUhE-clause text-1 TUhU-clause?

fragment <- prenex / terms VAU-clause? / ek / gihek / quantifier / NA-clause !JA-clause / relative-clauses / links / linkargs

prenex <- terms ZOhU-clause

;; sentence <- (terms CU-clause?)? bridi-tail / bridi-tail

sentence <- (terms CU-clause?)? bridi-tail

subsentence <- sentence / prenex subsentence

bridi-tail <- bridi-tail-1 (gihek stag? KE-clause bridi-tail KEhE-clause? tail-terms)?

bridi-tail-1 <- bridi-tail-2 (gihek !(stag? BO-clause) !(stag? KE-clause) bridi-tail-2 tail-terms)*

bridi-tail-2 <- bridi-tail-3 (gihek stag? BO-clause bridi-tail-2 tail-terms)?

bridi-tail-3 <- selbri tail-terms / gek-sentence

gek-sentence <- gek subsentence gik subsentence tail-terms / tag? KE-clause gek-sentence KEhE-clause? / NA-clause gek-sentence

tail-terms <- terms? VAU-clause?

terms <- terms-1+

terms-1 <- terms-2 (PEhE-clause joik-jek terms-2)*

terms-2 <- term (CEhE-clause term)*

;;term <- sumti / ( !gek (tag / FA-clause) (sumti / KU-clause?) ) / termset / NA-clause KU-clause

term <- term-1

term-1 <- sumti / ( !gek (tag / FA-clause) (sumti / KU-clause?) ) / termset / NA-clause KU-clause

termset <- gek-termset / NUhI-clause gek terms NUhU-clause? gik terms NUhU-clause? / NUhI-clause terms NUhU-clause?

gek-termset <- gek terms-gik-terms

terms-gik-terms <- term (gik / terms-gik-terms) term

sumti <- sumti-1 (VUhO-clause relative-clauses)?

sumti-1 <- sumti-2 (joik-ek stag? KE-clause sumti KEhE-clause?)?

sumti-2 <- sumti-3 (joik-ek sumti-3)*

sumti-3 <- sumti-4 (joik-ek stag? BO-clause sumti-3)?

sumti-4 <- sumti-5 / gek sumti gik sumti-4

sumti-5 <- quantifier? sumti-6 relative-clauses? / quantifier selbri KU-clause? relative-clauses?

sumti-6 <- ZO-clause / ZOI-clause / LOhU-clause / lerfu-string !MOI-clause BOI-clause? / LU-clause text LIhU-clause? / (LAhE-clause / NAhE-clause BO-clause) relative-clauses? sumti LUhU-clause? / KOhA-clause / LA-clause relative-clauses? CMENE-clause+ / (LA-clause / LE-clause) sumti-tail KU-clause? / li-clause

li-clause <- LI-clause mex LOhO-clause?

sumti-tail <- (sumti-6 relative-clauses?)? sumti-tail-1 / relative-clauses sumti-tail-1

sumti-tail-1 <- selbri relative-clauses? / quantifier selbri relative-clauses? / quantifier sumti

relative-clauses <- relative-clause (ZIhE-clause relative-clause)*

;; relative-clause <- GOI-clause term GEhU-clause? / NOI-clause subsentence KUhO-clause?

relative-clause <- relative-clause-1

relative-clause-1 <- GOI-clause term GEhU-clause? / NOI-clause subsentence KUhO-clause?

selbri <- tag? selbri-1

selbri-1 <- selbri-2 / NA-clause selbri

selbri-2 <- selbri-3 (CO-clause selbri-2)?

selbri-3 <- selbri-4+

selbri-4 <- selbri-5 (joik-jek selbri-5 / joik stag? KE-clause selbri-3 KEhE-clause?)*

selbri-5 <- selbri-6 ((jek / joik) stag? BO-clause selbri-5)?

selbri-6 <- tanru-unit (BO-clause selbri-6)? / NAhE-clause? guhek selbri gik selbri-6

tanru-unit <- tanru-unit-1 (CEI-clause tanru-unit-1)*

tanru-unit-1 <- tanru-unit-2 linkargs?

; ** zei is part of BRIVLA-clause
tanru-unit-2 <- BRIVLA-clause / GOhA-clause RAhO-clause? / KE-clause selbri-3 KEhE-clause? / ME-clause (sumti / lerfu-string) MEhU-clause? MOI-clause? / (number / lerfu-string) MOI-clause / NUhA-clause mex-operator / SE-clause tanru-unit-2 / JAI-clause tag? tanru-unit-2 / NAhE-clause tanru-unit-2 / NU-clause NAI-clause? (joik-jek NU-clause NAI-clause?)* subsentence KEI-clause?

;; linkargs <- BE-clause term links? BEhO-clause?

linkargs <- linkargs-1

linkargs-1 <- BE-clause term links? BEhO-clause?

;; links <- BEI-clause term links?

links <- links-1

links-1 <- BEI-clause term links?

quantifier <- number !MOI-clause BOI-clause? / VEI-clause mex VEhO-clause?

;;mex <- mex-1 (operator mex-1)* / rp-clause

mex <- mex-0

mex-0 <- mex-1 (operator mex-1)* / rp-clause

rp-clause <- FUhA-clause rp-expression

mex-1 <- mex-2 (BIhE-clause operator mex-1)?

mex-2 <- operand / mex-forethought

; This is just to make for clearer parse trees
mex-forethought <- PEhO-clause? operator fore-operands KUhE-clause?
fore-operands <- mex-2+ 

;li fu'a reboi ci pi'i voboi mu pi'i su'i reboi ci vu'u su'i du li rexa
;rp-expression <- rp-operand rp-operand operator
;rp-operand <- operand / rp-expression
; AKA (almost; this one allows a single operand; above does not.
;rp-expression <- rp-expression rp-expression operator / operand

; Right recursive version.
rp-expression <- operand rp-expression-tail
rp-expression-tail <- rp-expression operator rp-expression-tail / ()

;; operator <- operator-1 (joik-jek operator-1 / joik stag? KE-clause operator KEhE-clause?)*

operator <- operator-0

operator-0 <- operator-1 (joik-jek operator-1 / joik stag? KE-clause operator KEhE-clause?)*

operator-1 <- operator-2 / guhek operator-1 gik operator-2 / operator-2 (jek / joik) stag? BO-clause operator-1

operator-2 <- mex-operator / KE-clause operator KEhE-clause?

mex-operator <- SE-clause mex-operator / NAhE-clause mex-operator / MAhO-clause mex TEhU-clause? / NAhU-clause selbri TEhU-clause? / VUhU-clause

;; operand <- operand-1 (joik-ek stag? KE-clause operand KEhE-clause?)?

operand <- operand-0

operand-0 <- operand-1 (joik-ek stag? KE-clause operand KEhE-clause?)?

operand-1 <- operand-2 (joik-ek operand-2)*

operand-2 <- operand-3 (joik-ek stag? BO-clause operand-2)?

operand-3 <- quantifier / lerfu-string !MOI-clause BOI-clause? / NIhE-clause selbri TEhU-clause? / MOhE-clause sumti TEhU-clause? / JOhI-clause mex-2+ TEhU-clause? / gek operand gik operand-3 / (LAhE-clause / NAhE-clause BO-clause) operand LUhU-clause?

number <- PA-clause (PA-clause / lerfu-word)*

lerfu-string <- lerfu-word (PA-clause / lerfu-word)*

; ** BU clauses are part of BY-clause
lerfu-word <- BY-clause / LAU-clause lerfu-word / TEI-clause lerfu-string FOI-clause

ek <- NA-clause? SE-clause? A-clause NAI-clause?

;; gihek <- NA-clause? SE-clause? GIhA-clause NAI-clause?
gihek <- gihek-1

gihek-1 <- NA-clause? SE-clause? GIhA-clause NAI-clause?

jek <- NA-clause? SE-clause? JA-clause NAI-clause?

joik <- SE-clause? JOI-clause NAI-clause? / interval / GAhO-clause interval GAhO-clause

interval <- SE-clause? BIhI-clause NAI-clause?

;; joik-ek <- joik / ek
joik-ek <- joik-ek-1

joik-ek-1 <- (joik / ek)

joik-jek <- joik / jek

gek <- SE-clause? GA-clause NAI-clause? / joik GI-clause / stag gik

guhek <- SE-clause? GUhA-clause NAI-clause?

gik <- GI-clause NAI-clause?

tag <- tense-modal (joik-jek tense-modal)*

;stag <- simple-tense-modal ((jek / joik) simple-tense-modal)*
stag <- simple-tense-modal ((jek / joik) simple-tense-modal)* / tense-modal (joik-jek tense-modal)*

tense-modal <- simple-tense-modal / FIhO-clause selbri FEhU-clause?

simple-tense-modal <- NAhE-clause? SE-clause? BAI-clause NAI-clause? KI-clause? / NAhE-clause? ( ((time space? / space time?) CAhA-clause) / (time space? / space time?) / CAhA-clause ) KI-clause? / KI-clause / CUhE-clause

time <- ZI-clause time-offset* (ZEhA-clause (PU-clause NAI-clause?)?)? interval-property* / ZI-clause? time-offset+ (ZEhA-clause (PU-clause NAI-clause?)?)? interval-property* / ZI-clause? time-offset* ZEhA-clause (PU-clause NAI-clause?)? interval-property* / ZI-clause? time-offset* (ZEhA-clause (PU-clause NAI-clause?)?)? interval-property+

time-offset <- PU-clause NAI-clause? ZI-clause?

space <- VA-clause space-offset* space-interval? (MOhI-clause space-offset)? / VA-clause? space-offset+ space-interval? (MOhI-clause space-offset)? / VA-clause? space-offset* space-interval (MOhI-clause space-offset)? / VA-clause? space-offset* space-interval? MOhI-clause space-offset

space-offset <- FAhA-clause NAI-clause? VA-clause?

space-interval <- (VEhA-clause / VIhA-clause / VEhA-clause VIhA-clause) (FAhA-clause NAI-clause?)? space-int-props / (VEhA-clause / VIhA-clause / VEhA-clause VIhA-clause) (FAhA-clause NAI-clause?)? / space-int-props

space-int-props <- (FEhE-clause interval-property)+

interval-property <- number ROI-clause NAI-clause? / TAhE-clause NAI-clause? / ZAhO-clause NAI-clause?

free <- SEI-clause (terms CU-clause?)? selbri SEhU-clause? / SOI-clause sumti sumti? SEhU-clause? / vocative relative-clauses? selbri relative-clauses? DOhU-clause? / vocative relative-clauses? CMENE-clause+ relative-clauses? DOhU-clause? / vocative sumti? DOhU-clause? / (number / lerfu-string) MAI-clause / TO-clause text TOI-clause? / xi-clause

xi-clause <- XI-clause (number / lerfu-string) BOI-clause? / XI-clause VEI-clause mex VEhO-clause?

vocative <- ( COI-clause NAI-clause? )* DOI-clause / ( COI-clause NAI-clause? )+

indicators <- FUhE-clause? indicator+

indicator <-  ((UI-clause / CAI-clause) NAI-clause? / DAhO-clause / FUhO-clause) !BU-clause