/************************************************************** *abbrev.lex: A program to perform inline text replacement. *Copyright (C) 2004 Arnab Chakraborty * *This program is free software; you can redistribute it and/or modify *it under the terms of the GNU General Public License as published by *the Free Software Foundation; either version 2 of the License, or *(at your option) any later version. *This program is distributed in the hope that it will be useful, *but WITHOUT ANY WARRANTY; without even the implied warranty of *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *GNU General Public License for more details. * *You should have received a copy of the GNU General Public License *along with this program; if not, write to the Free Software *Foundation, Inc., 59 Temple Place, Suite 330, Boston, *MA 02111-1307 USA * *The author may be contacted by email at arnabc@stanfordalumni.org **************************************************************/ %{ #include #include #define MAX_ABBREV_LEN 30 #define MAX_EXPN_LEN 100 #define MAX_NO_OF_ABBREV 100 #define MAX_FILENAME_LEN 80 void addToBuffer(char c); void replace(char *str); void addRepl(char *from, char *to); char abbrev[MAX_ABBREV_LEN+1], expn[MAX_EXPN_LEN+1]; char abbrevLst[MAX_NO_OF_ABBREV][MAX_ABBREV_LEN+1], expnLst[MAX_NO_OF_ABBREV][MAX_EXPN_LEN+1]; int n = 0; int len; int count; %} %x inside word1 [a-zA-Z0-9]+ word2 [^\]]+ word3 [^ '\t\n.,\-+{}()]+ %% {word1}\[= { sscanf(yytext,"%[^\[]\[=",abbrev); count = 0; len = 0; expn[0] = '\0'; BEGIN(inside); } \] { expn[len]='\0'; addRepl(abbrev,expn); fprintf(yyout,"%s",expn); BEGIN(INITIAL); } \\\] {addToBuffer(']');} .|\n {addToBuffer(yytext[0]);} {word1} {replace(yytext);} %% void addToBuffer(char c) { if(len>28) { fprintf(stderr,"expnLstlacement too long (more than %d characters)", MAX_EXPN_LEN); exit(1); } expn[len++] = c; } void replace(char *str) { int i; for(i=0;i