#!/usr/bin/awk -f # Copyright (c) 2021 International Business Machines Corporation # Prepared by: Geert Janssen # Expects a C/C++ tokenizer generated CSV file as input with explicit # whitespace and separate newline (and continuation) tokens. # (tokenize -W -n [-N] -mcsv) # Outputs one possibly modified token (class or literal) per line. # Tries to use some context to better discriminate the meaning of some # otherwise ambiguous tokens. # Should use yacc/bison or lemon? # Ambiguous tokens in C/C++: # < > delimiters of filename in preprocessor include directive # Resolved by using preceding #include context # < > delimiters of template parameters # < less than operator # Resolve: preceding context keyword template, template <+ # > greater than operator # Resolve: preceding context keyword template < # " " delimiters of filename in preprocessor include directive # " " delimiters of string literal # Resolved by using preceding #include context # ( ) expression grouping # ( ) argument list # { } block # { } initializer # [ ] indexing # [ ] lambda capture # ~ destructor # ~ unary operator # - unary operator # - binary operator # Resolve: no white-space after - then unary? # * unary operator (dereference pointer) # * binary operator (multiplication) # * pointer declarator # & bitwise and operator # & address of operator # Can of worms: overloaded operator symbols # Simplistic CPP line syntax: # "#" directive-name (token)* newline # #include # #include "local" # #define identifier-macro-def # #define identifier-macro-const val # #define identifier-macro-func( ... ) # Using a stack to remember CSV token lines whose output is temporarily # suppressed. That way can have unbounded lookahead. # Use function to empty and print stack from bottom to top. function push(record) { stack[sp++]=record } function empty_out() { for (i=0; i disambiguation. (state == 0 && $4 == "template") { print $0 next_state=0 # switched off for now } # # seen; expect directive or identifier. (state == 1 && $3 == "identifier") { push($0) if ($4 in directive) { if ($4 == "include") next_state=2 else if ($4 == "define") next_state=7 else { empty_out() next_state=0 } } else { # #ident => stringize to "ident" empty_out() next_state=0 } } # Handle #include <... (state == 2 && $4 == "<") { # Note: suppressing this token. next_state=3 } # Handle #include "...". (state == 2 && $3 == "string") { # $4 has enclosing " doubled! filename=substr($4,3,length($4)-4) empty_out() print $1 "," $2 ",string-local-filename," filename next_state=0 } # Collect all tokens after the < till >. # Treat first specially to get its coordinates. (state == 3 && ($3 == "identifier" || $3 == "keyword")) { id_lin=$1 id_col=$2 filename=$4 # Note: modifying this token. next_state=4 } # Keep collecting tokens till > or newline. (state == 4 && $3 != "newline" && $4 != ">") { # eats up anything filename=filename $4 # Note: suppressing this token. next_state=4 } # Handling #include <...>, or #include <...newline. (state == 4 && ($3 == "newline" || $4 == ">")) { # When newline it's an error, but act as if > was present: empty_out() print id_lin "," id_col ",string-sys-filename,\"" filename "\"" if ($3 == "newline") print $0 # else suppressing the > token. next_state=0 } # Handle template <. (state == 5 && $4 == "<") { $3="start-template-paramlist" print $0 next_state=6 } # Handle template < >, explicit specialization. (state == 6 && $4 == ">") { $3="end-template-paramlist" print $0 next_state=0 } # Handle #define name. (state == 7 && ($3 == "identifier" || $3 == "keyword")) { id_lin=$1 id_col=$2 macro_name=$4 # Note: modifying this token later. next_state=8 } # Handle #define name(. (state == 8 && $4 == "(") { empty_out() print id_lin "," id_col ",identifier-macro-func," macro_name print $0 next_state=0 } # Handle #define name whitespace (state == 8 && $3 == "whitespace") { # Note: suppressing this token. next_state=9 } # Handle #define name whitespace? newline ((state == 8 || state == 9) && $3 == "newline") { empty_out() print id_lin "," id_col ",identifier-macro-def," macro_name print $0 next_state=0 } # Handle #define name whitespace !newline. (state == 9 && $3 != "newline") { empty_out() print id_lin "," id_col ",identifier-macro-const," macro_name print $0 next_state=0 } # Default rule; always executed: # 1. no prior rule matched: # - stay in same state only for whitespace, newline, and continuation; # this allows for their presence without explicit mention in rules # - output any previously suppressed tokens (to not lose them) # - print current token except for whitespace # - back to state 0 to quickly recover for any errors in input # 2. some rule matched: # - simply move on to next state as stated in that rule # - reset next_state to -1 { if (next_state == -1) { # Echo the current token as is (ignore whitespace though): if ($3 != "whitespace") { if ($3 != "newline" && $3 != "continuation") { empty_out() state=0 } print $0 } # otherwise: Do not change state! } else { state=next_state next_state=-1 } } END {}