Commit 9b572d88 authored by POGODALLA Sylvain's avatar POGODALLA Sylvain

Contectual management of keywords added (fixes #13)

parent 3248332d
......@@ -30,7 +30,7 @@ and
************
acgc is a "compiler" of ACG source code, i.e. files containing
definitions of signarures and lexicons. It basically checks whether
definitions of signatures and lexicons. It basically checks whether
they are correctly written (syntactically and wrt types and constant
typing) and outputs a .acgo object file. An interactive mode is
available to parse terms according to signatures.
......@@ -299,3 +299,17 @@ context-free lambda grammars).
```
lexicon my_new_lex = lex_2 << lex_1
3. Keywords
The keywords are "signature, "lexicon, "nl_lexicon", "end", "type",
"prefix", "infix", "binder", "lambda", and "Lambda".
The reserved symbols are '=', '<<', ';', ':', ',', '('), ')', '.',
'->', '=>', and ':='.
Inside a signature or a lexicon, "signature", "lexicon" and
"nl_lexicon" are not considered as keywords and can be used as
identifier.
Other keywords can be used as identifier when escaped with '\' (e.g.,
"\end").
......@@ -9,7 +9,7 @@ A list of related publications is available at the [ACG web page](http://calligr
## acgc
`acgc` is a "compiler" of ACG source code, i.e. files containing definitions of signarures and lexicons. It basically checks whether they are correctly written (syntactically and wrt types and constant typing) and outputs a `.acgo` object file. An interactive mode is available to parse terms according to signatures.
`acgc` is a "compiler" of ACG source code, i.e. files containing definitions of signatures and lexicons. It basically checks whether they are correctly written (syntactically and wrt types and constant typing) and outputs a `.acgo` object file. An interactive mode is available to parse terms according to signatures.
Run
```bash
......@@ -187,6 +187,7 @@ end
* `SYM t` if `SYM` is a prefix symbol (highest priority)
* `BINDER x y z.t` if `BINDER` is a binder
### Lexicons
There are two ways to define a lexicon:
......@@ -213,3 +214,13 @@ There are two ways to define a lexicon:
lexicon my_new_lex = lex_2 << lex_1
```
## Keywords
The keywords are "signature, "lexicon, "nl_lexicon", "end", "type", "prefix", "infix", "binder", "lambda", and "Lambda".
The reserved symbols are '=', '<<', ';', ':', ',', '('), ')', '.', '->', '=>', and ':='.
Inside a signature or a lexicon, "signature", "lexicon" and "nl_lexicon" are not considered as keywords and can be used as identifier.
Other keywords can be used as identifier when escaped with '\' (e.g., "\end").
; -*-org-*-
* DONE Add an optional ";" before the "end" keyword
* TODO Change lexers to avoid problems with keywords
* DONE Change lexers to avoid problems with keywords
* TODO Add precedence values and management in the signature
* TODO Add (+) notation style to prevent the infix use of +
* TODO Add syntactic extensions to handle associativity and precedence of infix operators
......@@ -10,3 +10,4 @@
* TODO change README and INSTALL files:
+ [X] to indicate new dependencies (menhir, logs...)
+ [ ] to document syntax extensions for infix operators, precedence of the latter over application. Highest precedence for prefix operators
* TODO What about lexicon composition when one lexicon at least is non-linear?
......@@ -38,8 +38,21 @@
| [] -> ()
| (p1,p2)::__ -> let () = brackets := [] in
raise (Error.Error (Error.Lexer_error (Error.Mismatch_parentheses,(p1,p2))))
type context =
| NoContext
| Signature
| Lexicon
| LexiconComposition
let ctx = ref NoContext
let set c = ctx:=c
let tok_in_context c t1 t2 =
if !ctx = c then t1 else t2
}
......@@ -49,6 +62,7 @@ let digit = ['0'-'9']
let string = (letter|digit|'_')*'\''*
let symbol = ['|' '!' '"' '#' '$' '%' '&' '\'' '*' '+' '-' '/' '<' '>' '?' '@' '[' '\\' ']' '^' '`' '{' '}' '~' ]
let keyword = ("end" | "type" | "prefix" | "infix" | "binder" | "lambda" | "Lambda")
rule lexer =
parse
......@@ -59,12 +73,34 @@ rule lexer =
| eof {
let () = check_brackets () in
Data_parser.EOI}
| "signature" as id {
let () = check_brackets () in
if !ctx = NoContext then
let () = set Signature in
Data_parser.SIG_OPEN(loc lexbuf)
else
IDENT(id,loc lexbuf)}
| "lexicon" as id {
let () = check_brackets () in
if !ctx = NoContext then
let () = set Lexicon in
Data_parser.LEX_OPEN(loc lexbuf)
else
IDENT(id,loc lexbuf)}
| "nl_lexicon" as id {
let () = check_brackets () in
if !ctx = NoContext then
let () = set Lexicon in
Data_parser.NL_LEX_OPEN(loc lexbuf)
else
IDENT(id,loc lexbuf)}
| ['='] {
let () = check_brackets () in
Data_parser.EQUAL(loc lexbuf)}
| "<<" {
let () = check_brackets () in
Data_parser.COMPOSE(loc lexbuf)}
let () = check_brackets () in
let () = set NoContext in
Data_parser.COMPOSE(loc lexbuf)}
| [';'] {
let () = check_brackets () in
Data_parser.SEMICOLON(loc lexbuf)}
......@@ -84,18 +120,12 @@ rule lexer =
Data_parser.RPAREN brac_loc}
| ['.'] {
Data_parser.DOT(loc lexbuf)}
| "signature" {
let () = check_brackets () in
Data_parser.SIG_OPEN(loc lexbuf)}
| "lexicon" {
let () = check_brackets () in
Data_parser.LEX_OPEN(loc lexbuf)}
| "nl_lexicon" {
let () = check_brackets () in
Data_parser.NL_LEX_OPEN(loc lexbuf)}
| "end" {
let () = check_brackets () in
Data_parser.END_OF_DEC(loc lexbuf)}
| '\\' keyword as id {
let () = check_brackets () in
Data_parser.IDENT(id,loc lexbuf)}
| "type" {
let () = check_brackets () in
Data_parser.TYPE(loc lexbuf)}
......@@ -135,3 +165,4 @@ rule lexer =
| _ {comment depth lexbuf}
......@@ -59,7 +59,7 @@ main: SIG_OPEN IDENT EQUAL IDENT COLON IDENT TYPE
## In state 122, spurious reduction of production sig_entry -> term_declaration
##
A type expression is expected after ":".
A type expression is expected after ':'.
term_alone: IDENT SEMICOLON
##
......@@ -160,7 +160,7 @@ sig_entry_eoi: BINDER TYPE
## BINDER
##
An identidier (the name of the binder) is expected after the keyword "binder".
An identidier (the name of the binder) is expected after the keyword 'binder'.
sig_entry_eoi: IDENT COLON IDENT TYPE
##
......@@ -180,7 +180,7 @@ sig_entry_eoi: IDENT COLON IDENT TYPE
## In state 122, spurious reduction of production sig_entry -> term_declaration
##
After a term declaration of the form "<term>: <type>", a semicolon ";" is expected.
After a term declaration of the form "<term>: <type>", a semicolon ';' is expected.
sig_entry_eoi: IDENT COLON SYMBOL
##
......@@ -215,7 +215,7 @@ sig_entry_eoi: IDENT COMMA TYPE
## IDENT COMMA
##
After a term or type declaration of the form "<ident1>, <ident2>", a type declaration of the form ": type;" (where type is a keyword) or a typing judgment of the form ": <type>;" is expected.
After a term or type declaration of the form "<ident1>, <ident2>", a type declaration of the form ": type;" (with the keyword 'type') or a typing judgment of the form ": <type>;" is expected.
sig_entry_eoi: IDENT EQUAL IDENT ARROW IDENT COLON SYMBOL
##
......@@ -227,7 +227,7 @@ sig_entry_eoi: IDENT EQUAL IDENT ARROW IDENT COLON SYMBOL
## IDENT EQUAL not_atomic_type_expression COLON
##
In a type definition, the keyword "type" is expected after the colon ":".
In a type definition, the keyword 'type' is expected after the colon ':'.
sig_entry_eoi: IDENT EQUAL IDENT ARROW IDENT TYPE
##
......@@ -246,7 +246,7 @@ sig_entry_eoi: IDENT EQUAL IDENT ARROW IDENT TYPE
## In state 53, spurious reduction of production not_atomic_type_expression -> atomic_type_or_term ARROW type_expression
##
In a type definition, a colon ":" is expeced before the keyword "type".
In a type definition, a colon ':' is expeced before the keyword 'type'.
sig_entry_eoi: IDENT EQUAL IDENT COLON SYMBOL
##
......@@ -259,7 +259,7 @@ sig_entry_eoi: IDENT EQUAL IDENT COLON SYMBOL
## type_or_term_definition_prefix
##
The "type" keyword or a typing judgement in the form of ": <type>;" is expected after the definition of a type or a term, resp.
The 'type' keyword or a typing judgement in the form of ": <type>;" is expected after the definition of a type or a term, resp.
sig_entry_eoi: IDENT EQUAL IDENT SEMICOLON
##
......@@ -279,7 +279,7 @@ sig_entry_eoi: IDENT EQUAL IDENT SEMICOLON
## In state 7, spurious reduction of production atomic_type_or_term -> IDENT
##
A typing judgement in the form of ": <type>;" or a type definition with a colon and the "type" keyword in the form of ": type;" is expectedin a term or a type definition.
A typing judgement in the form of ": <type>;" or a type definition with a colon ':' and the 'type' keyword in the form of ": type;" is expected in a term or a type definition.
sig_entry_eoi: IDENT EQUAL SYMBOL SYMBOL COLON TYPE
##
......@@ -325,7 +325,7 @@ sig_entry_eoi: IDENT EQUAL TYPE
## IDENT EQUAL
##
A definition in the form of "<term> : <type>;" or a type definition of the form "<type> : type;" is expected after a term or a type defintion, resp.
A definition in the form of "<term> : <type>;" or a type definition of the form "<type> : type;" (with the keyword 'type') is expected after a term or a type defintion, resp.
sig_entry_eoi: IDENT TYPE
##
......@@ -341,7 +341,7 @@ sig_entry_eoi: IDENT TYPE
## IDENT
##
A comma "," or a colon ":" are expected in a type or term declaration. An equality symbol "=" is expected in a type or term definition.
A comma ',' or a colon ':' are expected in a type or term declaration. An equality symbol '=' is expected in a type or term definition.
sig_entry_eoi: INFIX SYMBOL COLON TYPE
##
......@@ -380,7 +380,7 @@ sig_entry_eoi: PREFIX SYMBOL EQUAL IDENT COLON TYPE
## PREFIX SYMBOL EQUAL term COLON
##
A type is expected after the colon ":".
A type is expected after the colon ':'.
sig_entry_eoi: INFIX SYMBOL EQUAL IDENT SEMICOLON
##
......@@ -436,7 +436,7 @@ sig_entry_eoi: PREFIX SYMBOL EQUAL TYPE
## PREFIX SYMBOL EQUAL
##
A typing judgment in the form "term : <type>;" is expected.
A typing judgment in the form "<term> : <type>;" is expected.
sig_entry_eoi: INFIX SYMBOL TYPE
##
......@@ -494,7 +494,7 @@ sig_entry_eoi: TYPE
##
##
An identifier or a keyword ("infix", "prefix, or "binder") is expected.
An identifier or a keyword ('infix', 'prefix', or 'binder') is expected.
main: LEX_OPEN IDENT EQUAL IDENT COMPOSE TYPE
##
......@@ -537,7 +537,7 @@ main: LEX_OPEN IDENT EQUAL IDENT RPAREN
## In state 168, spurious reduction of production sig_or_lex -> lexicon
##
The composition operator "<<" is expected.
The composition operator '<<' is expected.
main: LEX_OPEN IDENT EQUAL LPAREN IDENT EOI
##
......@@ -555,7 +555,7 @@ main: LEX_OPEN IDENT EQUAL LPAREN IDENT EOI
## In state 159, spurious reduction of production lexicon_exp -> lexicon_exp0
##
The composition operator "<<" or a right parenthesis ")" is expected.
The composition operator '<<' or a right parenthesis ')' is expected.
main: LEX_OPEN IDENT EQUAL LPAREN TYPE
##
......@@ -619,7 +619,7 @@ main: LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT TYPE
## LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT
##
An equality symbold "=" is expected.
An equality symbold '=' is expected.
main: LEX_OPEN IDENT LPAREN IDENT RPAREN COLON TYPE
##
......@@ -673,7 +673,7 @@ main: LEX_OPEN IDENT LPAREN IDENT TYPE
## LEX_OPEN IDENT LPAREN IDENT
##
A right parenthesis ")" is expected.
A right parenthesis ')' is expected.
main: LEX_OPEN IDENT LPAREN TYPE
##
......@@ -698,7 +698,7 @@ main: LEX_OPEN IDENT TYPE
## LEX_OPEN IDENT
##
A left parenthesis "(" is expected.
A left parenthesis '(' is expected.
main: LEX_OPEN TYPE
##
......@@ -731,7 +731,7 @@ main: NL_LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT EQUAL SYMBOL COLON_EQUAL
## In state 68, spurious reduction of production lex_entry -> separated_nonempty_list(COMMA,id_or_sym) COLON_EQUAL atomic_type_or_term
##
A semi-colon ";" or the "end" keyword are expected.
A semi-colon ';' or the 'end' keyword are expected.
main: NL_LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT TYPE
##
......@@ -743,7 +743,7 @@ main: NL_LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT TYPE
## NL_LEX_OPEN IDENT LPAREN IDENT RPAREN COLON IDENT
##
An equality symbol "=" is expected.
An equality symbol '=' is expected.
main: NL_LEX_OPEN IDENT LPAREN IDENT RPAREN COLON TYPE
##
......@@ -767,7 +767,7 @@ main: NL_LEX_OPEN IDENT LPAREN IDENT RPAREN TYPE
## NL_LEX_OPEN IDENT LPAREN IDENT RPAREN
##
A colon ":" is expected.
A colon ':' is expected.
main: NL_LEX_OPEN IDENT LPAREN IDENT TYPE
##
......@@ -779,7 +779,7 @@ main: NL_LEX_OPEN IDENT LPAREN IDENT TYPE
## NL_LEX_OPEN IDENT LPAREN IDENT
##
A right parenthesis ")" is expected.
A right parenthesis ')' is expected.
main: NL_LEX_OPEN IDENT TYPE
##
......@@ -791,7 +791,7 @@ main: NL_LEX_OPEN IDENT TYPE
## NL_LEX_OPEN IDENT
##
A left parenthesis "(" is expected.
A left parenthesis '(' is expected.
main: NL_LEX_OPEN TYPE
##
......@@ -819,7 +819,7 @@ main: TYPE
##
##
A declaration of a signature (keyword "signature") or of a lexicon (keyword "lexicon" or "nl_lexicon") is expected.
A declaration of a signature (keyword 'signature') or of a lexicon (keyword 'lexicon' or 'nl_lexicon') is expected.
main: SIG_OPEN IDENT EQUAL IDENT COLON IDENT SEMICOLON TYPE
##
......@@ -853,7 +853,7 @@ main: SIG_OPEN IDENT TYPE
## SIG_OPEN IDENT
##
An equality symbol "=" is expected.
An equality symbol '=' is expected.
lex_entry_eoi: SYMBOL COLON_EQUAL IDENT ARROW TYPE
##
......@@ -912,7 +912,7 @@ lex_entry_eoi: SYMBOL COLON_EQUAL IDENT RPAREN
## In state 7, spurious reduction of production atomic_type_or_term -> IDENT
##
An arrow ("->" or "=>), a term, or a semi-colon are expected.
An arrow ('->' or '=>'), a term, or a semi-colon are expected.
lex_entry_eoi: SYMBOL COLON_EQUAL IDENT SEMICOLON
##
......@@ -960,7 +960,7 @@ lex_entry_eoi: SYMBOL COLON_EQUAL LPAREN IDENT ARROW IDENT RPAREN TYPE
## not_atomic_elementary_type
##
An arrow ("->" or "=>"), or a semi-colon are expected.
An arrow ('->' or '=>'), or a semi-colon are expected.
lex_entry_eoi: SYMBOL COLON_EQUAL LPAREN IDENT ARROW IDENT TYPE
##
......@@ -979,7 +979,7 @@ lex_entry_eoi: SYMBOL COLON_EQUAL LPAREN IDENT ARROW IDENT TYPE
## In state 53, spurious reduction of production not_atomic_type_expression -> atomic_type_or_term ARROW type_expression
##
An arrow ("->" or "=>"), a right parenthesis, or a semi-colon are expected.
An arrow ('->' or '=>'), a right parenthesis, or a semi-colon are expected.
heterogenous_term_and_type: IDENT COLON LPAREN IDENT TYPE
##
......@@ -993,7 +993,7 @@ heterogenous_term_and_type: IDENT COLON LPAREN IDENT TYPE
## LPAREN atomic_type_or_term
##
An arrow ("->" or "=>"), a right parenthesis, a term, or a semi-colon are expected.
An arrow ('->' or '=>'), a right parenthesis, a term, or a semi-colon are expected.
lex_entry_eoi: SYMBOL COLON_EQUAL LPAREN TYPE
##
......@@ -1062,7 +1062,7 @@ lex_entry_eoi: SYMBOL TYPE
## id_or_sym
##
A comma "," or an interpretation symbol ":=" are expected.
A comma ',' or an interpretation symbol ':=' are expected.
lex_entry_eoi: TYPE
##
......@@ -1142,7 +1142,7 @@ heterogenous_term_and_type: LPAREN SYMBOL RPAREN TYPE
## LPAREN not_atomic_term RPAREN
##
A term or a colon ":" are expected.
A term or a colon ':' are expected.
heterogenous_term_and_type: LAMBDA TYPE
##
......@@ -1176,7 +1176,7 @@ heterogenous_term_and_type: LAMBDA0 IDENT TYPE
## IDENT
##
An identifier (the name of a bound variable) or a dot "." are expected.
An identifier (the name of a bound variable) or a dot '.' are expected.
heterogenous_term_and_type: LPAREN IDENT SEMICOLON
##
......@@ -1263,7 +1263,7 @@ heterogenous_term_and_type: SYMBOL LPAREN SYMBOL SEMICOLON
## In state 30, spurious reduction of production not_atomic_term -> list(IDENT) SYMBOL list(term0)
##
A term or a right parenthesis ")" are expected.
A term or a right parenthesis ')' are expected.
heterogenous_term_and_type: LPAREN TYPE
##
......@@ -1297,7 +1297,7 @@ heterogenous_term_and_type: SYMBOL TYPE
## list(IDENT) SYMBOL
##
A term or a colon ":" are expected.
A term or a colon ':' are expected.
heterogenous_term_and_type: TYPE
##
......
......@@ -7,27 +7,8 @@
module Typing_env = Utils.StringSet
type toto = Environment.t
type context =
| Signature of Environment.Signature1.t
| Abs_and_obj of (Environment.Signature1.t * Environment.Signature1.t)
| Env of Environment.t
type type_or_cst =
| Type of (string*Abstract_syntax.location)
| Term of (string*Abstract_syntax.location)
let type_or_cst_to_string = function
| Type _ -> "Type"
| Term _ -> "Term"
exception Is_type
exception Is_cst
let emit_parse_error e loc = raise (Error.Error (Error.Parse_error (e,loc)))
let new_loc (s,_) (_,e) = (s,e)
let get_term_location = function
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment