Skip to content

Commit

Permalink
Improving Lesson 1.3 by mentioning longest match while scanning tokens (
Browse files Browse the repository at this point in the history
#3618)

Fixes #3539.

It makes more sense to me to add it to Lesson 1.3 instead of Lesson 1.4,
as we're introducing Parsing in this lesson. But I'm open to suggestions
to improve the text and where it should be! 😄

---------

Co-authored-by: rv-jenkins <[email protected]>
  • Loading branch information
Robertorosmaninho and rv-jenkins authored Sep 5, 2023
1 parent 413a22a commit 24d2b08
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions k-distribution/k-tutorial/1_basic/03_parsing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ interprets terms according to the grammar defined by the developer. It is
automatically converted into an AST of that term, and then the `colorOf`
function is evaluated using the function rules provided in the definition.

You can ask yourself: How does K match the strings between the double quotes?
The answer is that K uses Flex to generate a scanner for the grammar. Flex looks
for the longest possible match of a regular expression in the input. If there
are ambiguities between 2 or more regular expressions, it will pick the one with
the highest `prec` attribute. You can learn more about how Flex matching works
[here](https://westes.github.io/flex/manual/Matching.html#Matching).

Bringing us back to the file `lesson-03-a.k`, we can see that this grammar
has given a simple BNF grammar for expressions over Booleans. We have defined
constructors corresponding to the Boolean values true and false, and functions
Expand Down

0 comments on commit 24d2b08

Please sign in to comment.