I like doing this kind of thing all in one regex. This is incomplete, but should be enough to give you the basic idea.

#!/usr/bin/perl # https://perlmonks.org/?node_id=11105353 # following spirit of my http://www.rosettacode.org/wiki/Compiler/lexi +cal_analyzer#Alternate_Perl_Solution use strict; use warnings; my @tokens; my %reserved = map { $_ => 1 } qw( alignas alignof and and_eq asm atomic_cancel atomic_commit atomic_noexcept auto bitand bitor bool break case catch char char16_t char32_t class compl concept const constexpr const_cast continue co_await co_return co_yield decltype default delete do double dynamic_cast else enum explicit export extern false float for friend goto if import inline int long module mutable namespace new noexcept not not_eq nullptr operator or or_eq private protected public register reinterpret_cast requires return short signed sizeof static static_assert static_cast struct switch synchronized template this thread_local throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq ); my %Character = ( # Single characters by name '(' => 'LeftParen', ')' => 'RightParen', '[' => 'LeftSquare', ']' => 'RightSquare', '{' => 'LeftCurly', '}' => 'RightCurly', '<' => 'LessThan', '>' => 'GreaterThan', '=' => 'Equal', '+' => 'Plus', '-' => 'Minus', '*' => 'Asterisk', '/' => 'Slash', '#' => 'Hash', '.' => 'Dot', ',' => 'Comma', ':' => 'Colon', ';' => 'Semicolon', "'" => 'SingleQuote', '"' => 'DoubleQuote', '|' => 'Pipe', ); my %MultiOps = ( '>>' => 'RightShift', '<<' => 'LeftShift', '<=' => 'LessThanOrEqual', ); my $regex = qr/ \G (?| \s+ (?{ undef }) | \/\/.* (?{ undef }) | \d+(?:\.\d*)? (?{ 'Number' }) | \.\d+ (?{ 'Number' }) | \w+ (?{ $reserved{$&} ? 'reserved' : 'Identifier' }) | "([^"]*)" (?{ [ 'string', $1 ] }) | (?<!:)::(?!:) (?{ 'dblColon' }) | (?:<<|>>|<=) (?{ $MultiOps{$&} }) | . (?{ $Character{$&} or 'character' }) ) /x; $_ = join '', <DATA>; defined $^R and push @tokens, ref $^R ? $^R : [ $^R, $& ] while /$rege +x/gc; use Data::Dump 'dd'; dd @tokens; __DATA__ // comment should not appear main(void) { int foo = 1 << 5; puts("testing"); exit(0); // done }

Outputs:

( ["Identifier", "main"], ["LeftParen", "("], ["reserved", "void"], ["RightParen", ")"], ["LeftCurly", "{"], ["reserved", "int"], ["Identifier", "foo"], ["Equal", "="], ["Number", 1], ["LeftShift", "<<"], ["Number", 5], ["Semicolon", ";"], ["Identifier", "puts"], ["LeftParen", "("], ["string", "testing"], ["RightParen", ")"], ["Semicolon", ";"], ["Identifier", "exit"], ["LeftParen", "("], ["Number", 0], ["RightParen", ")"], ["Semicolon", ";"], ["RightCurly", "}"], )

At least it doesn't fail any of your provided test cases :)


In reply to Re: Lexing C++ by tybalt89
in thread Lexing C++ by Random_Walk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.