-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
grammar
modules with ready-to-use grammars
- Loading branch information
Showing
8 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pathlib import Path | ||
|
||
GRAMMAR_PATH = Path(__file__).parent / "grammars" | ||
|
||
|
||
def read_grammar(grammar_file_name, base_grammar_path=GRAMMAR_PATH): | ||
"""Read grammar file from default grammar path""" | ||
full_path = base_grammar_path / grammar_file_name | ||
with open(full_path) as file: | ||
return file.read() | ||
|
||
|
||
arithmetic = read_grammar("arithmetic.lark") | ||
json = read_grammar("json.lark") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
?start: sum | ||
|
||
?sum: product | ||
| sum "+" product -> add | ||
| sum "-" product -> sub | ||
|
||
?product: atom | ||
| product "*" atom -> mul | ||
| product "/" atom -> div | ||
|
||
?atom: NUMBER -> number | ||
| "-" atom -> neg | ||
| "(" sum ")" | ||
|
||
%import common.NUMBER | ||
%import common.WS_INLINE | ||
|
||
%ignore WS_INLINE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Adapted from https://github.com/lark-parser/lark/blob/master/lark/grammars/common.lark | ||
|
||
// Lark License: | ||
// Copyright © 2017 Erez Shinan | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
|
||
// Basic terminals for common use | ||
|
||
|
||
// | ||
// Numbers | ||
// | ||
|
||
DIGIT: "0".."9" | ||
HEXDIGIT: "a".."f"|"A".."F"|DIGIT | ||
|
||
INT: DIGIT+ | ||
SIGNED_INT: ["+"|"-"] INT | ||
DECIMAL: INT "." INT? | "." INT | ||
|
||
// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/ | ||
_EXP: ("e"|"E") SIGNED_INT | ||
FLOAT: INT _EXP | DECIMAL _EXP? | ||
SIGNED_FLOAT: ["+"|"-"] FLOAT | ||
|
||
NUMBER: FLOAT | INT | ||
SIGNED_NUMBER: ["+"|"-"] NUMBER | ||
|
||
// | ||
// TODO: Working escaped_string | ||
// | ||
UNESCAPED_STRING: /\"[^"]*\"/ | ||
|
||
|
||
|
||
// | ||
// Names (Variables) | ||
// | ||
LCASE_LETTER: "a".."z" | ||
UCASE_LETTER: "A".."Z" | ||
|
||
LETTER: UCASE_LETTER | LCASE_LETTER | ||
WORD: LETTER+ | ||
|
||
CNAME: ("_"|LETTER) ("_"|LETTER|DIGIT)* | ||
|
||
|
||
// | ||
// Whitespace | ||
// | ||
WS_INLINE: (" "|/\t/)+ | ||
WS: /[ \t\f\r\n]/+ | ||
|
||
CR : /\r/ | ||
LF : /\n/ | ||
NEWLINE: (CR? LF)+ | ||
|
||
|
||
// Comments | ||
SH_COMMENT: /#[^\n]*/ | ||
CPP_COMMENT: /\/\/[^\n]*/ | ||
C_COMMENT: "/*" /(.|\n)*?/ "*/" | ||
SQL_COMMENT: /--[^\n]*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
?start: value | ||
|
||
?value: object | ||
| array | ||
| UNESCAPED_STRING | ||
| SIGNED_NUMBER -> number | ||
| "true" -> true | ||
| "false" -> false | ||
| "null" -> null | ||
|
||
array : "[" [value ("," value)*] "]" | ||
object : "{" [pair ("," pair)*] "}" | ||
pair : UNESCAPED_STRING ":" value | ||
|
||
%import common.UNESCAPED_STRING | ||
%import common.SIGNED_NUMBER | ||
%import common.WS | ||
|
||
%ignore WS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
import outlines.grammars as grammars | ||
from outlines.fsm.fsm import CFGFSM | ||
|
||
|
||
@pytest.mark.parametrize("grammar", [grammars.json, grammars.arithmetic]) | ||
def test_grammar_module(grammar): | ||
class MockTokenizer: | ||
vocabulary = {"(": 1, ")": 2, "a": 3, "eos": 4} | ||
special_tokens = {"eos"} | ||
eos_token = "eos" | ||
eos_token_id = 4 | ||
|
||
def convert_token_to_string(self, token): | ||
return token | ||
|
||
@property | ||
def inverse_vocabulary(self): | ||
return {v: k for k, v in self.vocabulary.items()} | ||
|
||
def decode(self, token_ids): | ||
return [self.inverse_vocabulary[t] for t in token_ids] | ||
|
||
cfg_str = """ | ||
start: s | ||
s: "(" s ")" | /a+/ | ||
""" | ||
tokenizer = MockTokenizer() | ||
fsm = CFGFSM(cfg_str, tokenizer) | ||
assert isinstance(fsm, CFGFSM) |