Skip to content

Instantly share code, notes, and snippets.

@bbrowning
Created May 22, 2025 12:38
Show Gist options
  • Save bbrowning/125765c6f0c4f35f0d0ba92ee0559e93 to your computer and use it in GitHub Desktop.
Save bbrowning/125765c6f0c4f35f0d0ba92ee0559e93 to your computer and use it in GitHub Desktop.
EBNF grammar (for use with Tatsu) for Llama 4 Pythonic tool calling parsing
@@grammar::Llama4
start
=
expression $
;
expression
=
[ start_python_tag ] functions:function_list [ end_python_tag ]
;
start_python_tag
=
"<|python_start|>"
;
end_python_tag
=
"<|python_end|>"
;
function_list
=
[ "[" ] /(,|;)/%{ @+:function }+ [ "]" ]
;
function
=
name:function_name "(" ","%{ args+:( function_args | () ) }+ ")"
;
function_name
=
python_identifier
;
function_args
=
key:python_identifier "=" value:arg_value
;
arg_value
=
| bool:bool
| number:number
| string:string
| dict:python_dict
| list:python_list
| identifier:python_identifier
;
bool
=
| "True"
| "true"
| "False"
| "false"
;
number
=
/-?\d+\.?\d*/
;
string
=
# strings like: "foobarbaz"
| '"' /[^"]*/ '"'
# strings like: 'Martha\'s Vineyard'
| "'" ( "\" "'" )%{ /[^'\\]+/ } "'"
# strings like: '\"cool units\"'
| "'" "\" '"' /[^'\\]+/ "\" '"' "'"
# strings like: 'foo\"bar\"baz'
| "'" ( "\" '"' )%{ /[^'\\]+/ } "'"
;
python_dict
=
"{" @:",".{ python_dict_kv } "}"
;
python_dict_kv
=
key:string ":" value:arg_value
;
python_list
=
"[" @:",".{ arg_value } "]"
;
python_identifier
=
/[a-zA-Z_][a-zA-Z_\-0-9]*/
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment