Title: Theory / Unimportant / CS / Programming / oCaml / Manual{ / 13 Lexer and Parser / Yacc;
Tags: récupéré declaration computer_to_instruction fromCommandLine hasNotNewEffectReturnsMeaning FromInstruction mid1516974248815
On every note of this deck, unless a note says otherwise:
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.

Extension: mly
Description: ocaml grammar
Means: ml yacc
Other fields (not shown on cards):
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Chapter: 13
Section: 3

Name: Lexer definition
Definition: {header} (optional)
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Chapter: 13
Section: 2
Context: ocamllex
Kind: mly
Definition2: let ident = regexp
Definition3: [refill {refill-handler}] optional
Definition4: rule entrypoint [arg_1… arg_n] =
  parse regexp { action }
      | …
      | regexp { action }
and entrypoint [arg_1… arg_n] =
  parse …
and …
Definition5: {trailer} (optional)

Instruction: {
foo
} in top of file
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: copy foo in the top of the output
Language: ocamllex
Chapter: 13
Section: 2.1
Returns: open directive

Instruction: {
foo
} in bottom of file
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: copy foo in the bottom of the output
Language: ocamllex
Chapter: 13
Section: 2.1

Instruction: let foo = bar
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Meaning: Give name foo to reguar expression bar
Language: ocamllex
Chapter: 13
Section: 2.2
Identifier: foo
Where to use it: between header and rule

Instruction: rule foo bar_1 ... bar_n =
   parse buz_1 {xup_1}
           | ...
           | buz_m {xup_m}
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Meaning: Introduce a function foo taking n+1 arguments. The n first beeing bar_i, the last one being a stream.
for pref the longest prefix matched by some buz_i
pref is consummed, and xup_i is executed
i assumed minimal in case of ambiguity
Language: ocamllex
Chapter: 13
Section: 2.2
Context: rule part
Identifier: foo

Instruction: rule foo bar_1 ... bar_n =
   shortest buz_1 {xup_1}
           | ...
           | buz_m {xup_m}
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Meaning: Introduce a function foo taking n+1 arguments. The n first beeing bar_i, the last one being a stream.
for pref the shortest prefix matched by some buz_i
pref is consummed, and xup_i is executed
i assumed minimal in case of ambiguity
Language: ocamllex
Chapter: 13
Section: 2.2
Context: rule part
Identifier: foo

Instruction: eof
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: end of file
Language: ocamllex
Context: regexp
Chapter: 13
Section: 2.4

Instruction: _
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: match any char
Language: ocamllex
Context: regexp
Chapter: 13
Section: 2.4

Instruction: foo as bar
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: call bar the substring matched by the regexp foo
Language: ocamllex
Context: regexp
Chapter: 13
Section: 2.4

Instruction: lexbuf
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: the currently lexed buffer
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5

Instruction: Lexing.lexeme lexbuf
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: the matched string
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5

Instruction: Lexing.lexeme_char lexbuf foo
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: the foo-th matched string
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5

Instruction: Lexing.lexeme_start lexbuf
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: the position of the first character of the matched string in the stream
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5

Instruction: Lexing.lexeme_end lexbuf
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: the position of the successor of the last character of the matched string in the stream
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5

Instruction: foo bar_1 ... bar_n lexbuf
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: call recursively another entrypoint foo with parameter bar_i, and current lexer
Language: ocamllex
Context: action
Chapter: 13
Section: 2.5
Returns: recurvive comment, with depth as parameter

Instruction: lexbuf.refull_buff
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: refill the buffer when it is empty
Language: ocamllex
Context: action
Chapter: 13
Section: 2.6

Name: refill-handler
Definition: Function that is called when refill occurs. Takse as argument the usual process
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Chapter: 13
Section: 2.6
Context: ocamllex action
Typ: (Lexing.lexbuf -> 'a) -> Lexing.lexbuf -> 'a

Instruction: ocamlyacc bar
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Effect: compile the grammar bar
Language: ocamlyacc
Chapter: 13
Section: 3

Name: grammar definition
Definition: %{
header
%} optional
(comment as in ocaml)
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Chapter: 13
Section: 4
Context: ocamlyacc
Definition2: declarations (comment as in c)
Conjdef: In this order
Definition3: %%
rules (comment as in c)
Definition4: %%
trailer (comment as in ocaml)

Name: grammar definition
Definition: %{
 header
%} optional
(comment as in ocaml)
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html
Chapter: 13
Section: 4
Context: ocamlyacc

Instruction: %{
foo
%}
in top of file
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Effect: foo is literally copied in the beginning of the output
Language: ocamlyacc
Chapter: 13
Section: 41
Name: header

Instruction: %{
foo
%}
in bottom of file
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Effect: foo is literally copied in the bottom of the output
Language: ocamlyacc
Chapter: 13
Section: 4.1
Intuition: trailer

Instruction: %token foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: declaration
Identifier: foo_1 ... foo_n
Typ: tokens

Instruction: %token type foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: declaration
Identifier: foo_1 ... foo_n
Assuming: type is a fully qualified type
Typ: tokens of type type
Extra: difference with %type bar foo_1 ... foo_n?

Instruction: %start foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that each symbol foo_i are entry points of the grammar
A parsing function is generated in the output module for each of them
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: declaration
Identifier: foo_1 ... foo_n

Instruction: %type bar foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that the symbols foo_i's type is bar
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: declaration
Identifier: foo_1 ... foo_n
Assuming: bar fully qualified type

Instruction: %left foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that the symbols foo_i's are left associative
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: declaration
Assuming: bar fully qualified  type
Intuition: trailer

Instruction: %right foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that the symbols foo_i's are right associative
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: Declaration

Instruction: %nonassoc foo_1 ... foo_n
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that the symbols foo_i's are not associative
Language: ocamlyacc
Chapter: 13
Section: 4.2
Context: Declaration

Smaller0: the priority of foo is greater than the one of bar
Greater0: foo' associativity is given before the one of bar
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Chapter: 13
Section: 4.2
Variables: foo bar symbols
Smaller to greater: iff
Context: ocamlyacc declaration

Question: In a rule reduce conflict, the choose one is:
Answer: the one with greater priority
Chapter: 13
Section: 4.2
Variables: t a token
r a rule
Answer2: if impossible then:
if the token is left associative, then token is shiftel
If the token is right associative, rule reduction
If the token is non associative, syntax error
Answer3: otherwise, shift and warning
Answer7: iff

Question: In a reduce/reduce conflict, the choosen one is:
Answer: first one in declaration of the file. And a warning is emitted
Chapter: 13
Section: 4.2
Variables: t a token
r a rule

Instruction: foo:
  | bar_11 ... bar_1n_1 {buz_1}
  | ...
  | bar_m1 ... bar_mn_m {buz_m}
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Meaning: declare that foo is a rule, such that when some bar_i1 ... bar_in_i is seen in the stream, action buz_i must be done
Language: ocamlyacc
Chapter: 13
Section: 4.3
Identifier: foo

Instruction: $i
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Effect: the token corresponding to the i-th symbol
Language: ocamlyacc
Context: Rule action
Chapter: 13
Section: 4.3

Instruction: error
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Effect: resynchronization point
Language: ocamlyacc
Context: Rule
Chapter: 13
Section: 4.3

Question: When no rule apply
Answer: parse_error "syntax error" is called
Chapter: 13
Section: 4.3
Context: ocamlyacc rule

Variable: parse_error
Meaning: function called when no rule apply or a Parsing.Parse_error exception is raied in an action
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Language: ocamlyacc
Section: 4.4
Chapter: 13
Initialization: do nothing and return

Instruction: Parsing.Parse_error
Page: https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html.
Effect: Error related to parsing
Language: ocamlyacc
Chapter: 13
Section: 4.4

Question: Parsing in error mode
Answer: Everything is discarded until reach can be shifted. Then it discard everything until 3 token can be accepted. It starts in normal mode, with those three states
Chapter: 13
Section: 4.4
Context: ocamlyacc

Fla: -bfoo
Effect: name the ouptu foo.ml .mli and .output
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: -q
Effect: no effect
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: -v
Effect: Generate a description of the parsing tables and a report on conflicts resulting from ambiguities in the grammar. The description is put in file grammar.output.
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: -version
Effect: print version string and exit
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: -vnum
Effect: short version number and exit
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: -
Effect: read grammar specification from stdin
Prefix: ocamlyacc
Chapter: 13
Section: 4.4

Fla: --foo
Effect: state that foo is the input, even if it starts with a -
Prefix: ocamlyacc
Chapter: 13
Section: 4.4
Assuming: last command on command lime

Question: How to recognize keywords/reserved words
Answer: Do not list them. Recognize them as arbitrary string, and test using an hashtable whethe it belong to the list
Chapter: 13
Section: 7
Context: ocamlyacc