Skip to content

Instantly share code, notes, and snippets.

@wei2912
Created January 10, 2015 12:53
Show Gist options
  • Select an option

  • Save wei2912/b014b8d9b1d62fd76a75 to your computer and use it in GitHub Desktop.

Select an option

Save wei2912/b014b8d9b1d62fd76a75 to your computer and use it in GitHub Desktop.
shn.l
%{
#include <stdio.h>
#include <string.h>
enum yytokentype {
COMMA = 258,
SEMICOLON,
LBRACKET,
RBRACKET,
LBRACE,
RBRACE,
FULLSTOP,
COLON,
IDENTIFIER,
TAG,
WORD,
REGEXP,
DEFSYMBOL,
DEFPAR,
DEFREGEXP,
DEFDICT
};
char *yylval;
%}
ws [ \t\n\v\f\r]
ident [A-Za-z][A-Za-z0-9_]*
word [^.:);]+
regexp [^;]+
comment "#".+?"\n"
%s defsymbol
%s defpar
%s defregexp
%s defdict
%s words
%s word
%%
<*>{comment} {}
<*>{ws}+ {}
<INITIAL>"%defsymbol" {
BEGIN(defsymbol);
return DEFSYMBOL;
}
<INITIAL>"%defpar" {
BEGIN(defpar);
return DEFPAR;
}
<INITIAL>"%defdict" {
BEGIN(defdict);
return DEFDICT;
}
<defsymbol>{ident} {
yylval = strdup(yytext);
return IDENTIFIER;
}
<defsymbol>"," { return COMMA; }
<defsymbol>";" {
BEGIN(INITIAL);
return SEMICOLON;
}
<defpar>{ident} {
yylval = strdup(yytext);
return IDENTIFIER;
}
<defpar>"{" {
BEGIN(words);
return LBRACE;
}
<defdict>{ident} {
yylval = strdup(yytext);
return IDENTIFIER;
}
<defdict>"{" {
BEGIN(words);
return LBRACE;
}
<words>"}" {
return RBRACE;
}
<words>";" {
BEGIN(INITIAL);
return SEMICOLON;
}
<words>"(" {
BEGIN(word);
return LBRACKET;
}
<words>"%regexp" {
BEGIN(defregexp);
return DEFREGEXP;
}
<word>"."{ident} {
yylval = strdup(yytext + 1);
return TAG;
}
<word>{word} {
yylval = strdup(yytext);
return WORD;
}
<word>"." { return FULLSTOP; }
<word>":" { return COLON; }
<word>")" { return RBRACKET; }
<word>";" {
BEGIN(words);
return SEMICOLON;
}
<defregexp>{regexp} {
yylval = strdup(yytext);
return REGEXP;
}
<defregexp>";" {
BEGIN(words);
return SEMICOLON;
}
%%
int main(int argc, char **argv) {
int tok;
while (tok = yylex()) {
printf("%d", tok);
if (tok == IDENTIFIER || tok == WORD || tok == REGEXP) {
printf(" %s", yylval);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment