summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Siraphob <bensiraphob@gmail.com>2023-09-01 09:32:14 -0400
committerGitHub <noreply@github.com>2023-09-01 16:32:14 +0300
commitd8d6977f48f1fa402ade38ad32c5b5fb1358d059 (patch)
treefff20270844255c734f3abca8aaa42b2944f5d8a
parent5aec2cfaac386eb09aebb75b805860828f00de91 (diff)
examples : add C grammar (#2357)
-rw-r--r--grammars/c.gbnf42
1 files changed, 42 insertions, 0 deletions
diff --git a/grammars/c.gbnf b/grammars/c.gbnf
new file mode 100644
index 00000000..4a0331dd
--- /dev/null
+++ b/grammars/c.gbnf
@@ -0,0 +1,42 @@
+root ::= (declaration)*
+
+declaration ::= dataType identifier "(" parameter? ")" "{" statement* "}"
+
+dataType ::= "int" ws | "float" ws | "char" ws
+identifier ::= [a-zA-Z_] [a-zA-Z_0-9]*
+
+parameter ::= dataType identifier
+
+statement ::=
+ ( dataType identifier ws "=" ws expression ";" ) |
+ ( identifier ws "=" ws expression ";" ) |
+ ( identifier ws "(" argList? ")" ";" ) |
+ ( "return" ws expression ";" ) |
+ ( "while" "(" condition ")" "{" statement* "}" ) |
+ ( "for" "(" forInit ";" ws condition ";" ws forUpdate ")" "{" statement* "}" ) |
+ ( "if" "(" condition ")" "{" statement* "}" ("else" "{" statement* "}")? ) |
+ ( singleLineComment ) |
+ ( multiLineComment )
+
+forInit ::= dataType identifier ws "=" ws expression | identifier ws "=" ws expression
+forUpdate ::= identifier ws "=" ws expression
+
+condition ::= expression relationOperator expression
+relationOperator ::= ("<=" | "<" | "==" | "!=" | ">=" | ">")
+
+expression ::= term (("+" | "-") term)*
+term ::= factor(("*" | "/") factor)*
+
+factor ::= identifier | number | unaryTerm | funcCall | parenExpression
+unaryTerm ::= "-" factor
+funcCall ::= identifier "(" argList? ")"
+parenExpression ::= "(" ws expression ws ")"
+
+argList ::= expression ("," ws expression)*
+
+number ::= [0-9]+
+
+singleLineComment ::= "//" [^\n]* "\n"
+multiLineComment ::= "/*" ( [^*] | ("*" [^/]) )* "*/"
+
+ws ::= ([ \t\n]+)