Commit graph

438 commits

Author SHA1 Message Date
Charles Baker
3d1351c555 Add column information to errors 2023-07-16 15:30:55 +12:00
Charles Baker
d0baae1ccd LINT: Formatting around adding columns to errors in Grammar.[ch]pp 2023-07-16 15:29:53 +12:00
Charles Baker
eb228024db Calculate line and column inside GrammarParser::error() for simplicity 2023-07-16 15:29:53 +12:00
mingodad
29d91e8ec8 Add column info to GrammarSymbol and error messages 2023-07-16 15:29:53 +12:00
mingodad
16ebbc5683 Add column info to error messages in ErrorPolicy 2023-07-16 15:29:53 +12:00
mingodad
4aca034e26 Add 'std::' in several places as suggested by clang 2023-07-15 14:20:07 +12:00
Charles Baker
aeacb29cb2 Fix maximum calculation in GrammarSymbolSet::insert()
Fixes #36 "Improve performance of GrammarSymbolSet".  Not reproduced
exactly because I didn't have the grammar to hand but it seems likely
this is the problem.
2023-06-08 05:24:01 +12:00
Charles Baker
b0a6fb6b24 Track and iterate only over set range in GrammarSymbolSet
FIX: Iterate over all elements when combining sets
2023-06-03 11:53:31 +12:00
Charles Baker
b72f97e34d Use size_t for set elements in GrammarSymbolSet 2023-06-03 11:53:30 +12:00
Charles Baker
93160d6200 Avoid creating temporary strings adding symbols to Grammar 2023-06-03 11:52:52 +12:00
Charles Baker
6821f786b6 Add character encoding examples and documentation, e.g. parsing UTF-8 2023-06-03 11:50:15 +12:00
Charles Baker
e346baf911 Update README.md to describe parsing with different encodings 2023-06-03 11:45:55 +12:00
Charles Baker
071273032e Update XML example to parser from UTF-8 to UTF-8 in memory 2023-06-03 11:45:55 +12:00
Charles Baker
0dc846f6eb Update JSON example to parse from a UTF-8 encoded JSON file
This is an example of using Lalr to parse UTF-8 encoded input.
2023-06-03 11:45:55 +12:00
Charles Baker
d03a02d3e5 Use correct string for lexeme when debugging parsers
This works for UTF-8 encoded strings but debugging will fail when using
UTF-16 or UTF-32 as the printf() formatted string won't deal with null
characters embedded within the wider character encodings.

Committing this for now because it doesn't break any existing
functionality but does enable parsing of wider character inputs.
2023-05-29 21:20:23 +12:00
Charles Baker
1f85c9d00a Explicitly link with pthread on Linux 2023-05-28 20:49:51 +12:00
Charles Baker
3d31f45a0f Update lalr_examples/json.cpp now that arrays are parsed. 2023-05-28 20:46:43 +12:00
Charles Baker
9ce34162d1 Inline trivial methods in parser generator 2023-05-28 20:46:43 +12:00
Charles Baker
1f572973c3 Reduce memory usage by omitting labels in normal parsers
Labels are only used to add meaningful text to states in Graphviz DOT
graphs.  But these strings can take lots of time and memory to generate
especially in large grammars like that for PostgreSQL.  Therefore only
generate labels when they will be used.
2023-05-28 20:45:44 +12:00
Charles Baker
c7eea3b698 Reduce memory usage by removing vector from GrammarSymbolSet 2023-05-28 20:45:44 +12:00
Charles Baker
c9a6a297bf Sanitize parser state labels 2023-05-28 20:45:44 +12:00
Charles Baker
a5db5713da Remove unused GrammarSymbolLess 2023-05-28 20:45:44 +12:00
Charles Baker
faaafdcef6 Remove GrammarProduction pointer from GrammarItem 2023-05-28 20:45:44 +12:00
Charles Baker
9d90804ae2 Use a vector of active GrammarStates to track new states
The previous algorithm would loop through all states but only process
those that didn't have their processed flag set.  Much faster to only
iterate through those states that haven't been processed.
2023-05-26 22:07:34 +12:00
Charles Baker
ebe173cb12 Parse arrays in the JSON example 2023-05-26 21:53:01 +12:00
Charles Baker
c5a7c1728d Generate errors for missing lexical action handlers 2023-05-21 21:00:37 +12:00
Charles Baker
77602f9052 Test that parsers with missing lexical action handlers generate errors 2023-05-21 20:15:29 +12:00
Charles Baker
d8948dd936 Report missing action handlers from Parser::valid() 2023-05-21 20:15:12 +12:00
Charles Baker
184a8b34c0 Report missing lexical action handlers from Lexer::valid() 2023-05-21 20:04:14 +12:00
Charles Baker
e56556f390 Move Parser::fire_{error,print}() and Parser::is_debug_enabled() to const member section 2023-05-21 20:03:36 +12:00
Charles Baker
b7b7c4d61e Move Lexer::set_action_handler() to non-const member section 2023-05-21 19:53:56 +12:00
Charles Baker
5ce357e651 Treat empty character classes in a regular expression as an error 2023-05-20 22:15:47 +12:00
Charles Baker
aaea898aab Allow most metacharacters in character classes, e.g. allow "[+-]"
Allows most metacharacters, e.g. "*+()[", to appear unescaped within a
character class.  This, along with hyphens at the end, allows "[+-]" to
match a plus or a minus.
2023-05-20 22:15:47 +12:00
Charles Baker
2235e9fe6b Stop Lexer reading past end of input when skipping whitespace 2023-05-20 21:01:39 +12:00
Charles Baker
eb23625604 Simplify lexer state generation in RegexGenerator 2023-05-19 20:15:01 +12:00
Charles Baker
0e8a1e96b6 Declare write_graphviz_dot() in dot.hpp, include from lalrc.cpp 2023-05-19 20:15:01 +12:00
Charles Baker
cee6d1372a Rename RegexState::get_index() to RegexState::index() 2023-05-19 20:15:01 +12:00
Charles Baker
76d51795f3 Remove trailing newline from C++ output 2023-05-19 20:15:01 +12:00
Charles Baker
02210884a3 Implement RegexItem::operator<() with std::lexicographical_compare()
Using set<>::operator<() doesn't generate a stable comparison wrt. the
nodes in the item.  This is either set<>::operator<() not behaving as
a lexicographical compare like I expect or not taking into account the
comparison object the set is initialized with.

Fixes non-deterministic lexer generation.
2023-05-19 20:15:01 +12:00
Charles Baker
6750cb1769 Store ParserSymbol in whitespace lexer tokens matched symbol
Previously this cast a GrammarSymbol to a ParserSymbol and then wrote
out its index -- generating a wildly wrong value.  This was okay at
runtime because the lexer only checks to see whether or not there is a
symbol and never uses its value.

However it looks like a pretty nasty error to anyone looking at the
generated source code.  Fixed by converting the GrammarSymbol to the
correct ParserSymbol when the lexer is written out.
2023-05-12 21:07:11 +12:00
Charles Baker
7f1b4c97c1 Optimize spontaneous and propagated lookahead in parser generator
Uses the algorithms described in Compilers - Principles, Techniques,
and Tools, p.241 to generate spontaneous lookaheads and propagate them
through the state machine.

Adds GrammarSymbolSet to store symbol sets as a bitmask by symbol index
and a vector of GrammarSymbol*.

Multi-threads the calculation of goto items for lookahead propagation.
2023-05-10 20:41:14 +12:00
Charles Baker
2a442d2667 Write parser state machines as GraphViz dot graphs 2023-04-21 20:23:47 +12:00
Charles Baker
c77e4d5a7b Only allocate GrammarState when needed in GrammarGenerator::goto_ 2023-04-18 15:17:14 +12:00
Charles Baker
85472d7b9e Use vector of transitions by symbol index for transitions between states 2023-04-18 15:17:14 +12:00
Charles Baker
6b4285bc78 Bump standard to c++14 for std::make_unique() 2023-04-18 15:06:55 +12:00
Charles Baker
3c810e1a74 Generate state machine from kernel items only 2023-04-17 13:26:43 +12:00
Charles Baker
390e8fc8f1 Add lalr library to all build target 2023-04-17 13:08:47 +12:00
Charles Baker
5a0e55bddf Use set not std::set in GrammarGenerator 2023-04-17 13:06:53 +12:00
Charles Baker
7d1b6cd2a2 Take precedence and associativity from either form of an implicit terminal
Sets the precedence and associativity of an implicit terminal from the
literal or identifier that sets it.  It's an error for both the terminal
and the identifier to set it.
2023-04-01 20:04:47 +13:00
Charles Baker
b0e68cf7a9 Check that implicit terminals only specify associativity on one symbol
Implicit terminals can be identified by their literal value or by the
identifier that names them.  But if both forms are used to specify an
associativity and precedence then it's not clear which one is correct.
Make it an error if associativity is specified for a terminal by its
literal and its identifier.
2023-04-01 20:04:47 +13:00