Demo entry 6197182

ㅇㅇㅇ

   

Submitted by anonymous on Oct 11, 2016 at 18:29
Language: C++. Code size: 3.1 kB.

%{
#include <math.h>
#include <stdlib.h>
#include <regex.h>
#include <stdio.h>
#include <ctype.h>
#include "y.tab.h"
#define PAGE_SIZE 1024
#define MATCH_SIZE 1
%}
%%

[0-9]+	{
		yylval = atoi(yytext);
		return NUMBER;
	}

[-+()=/*]	{
			return *yytext; 
		}

[ \t]	;

[\n]	{ return 0; }

%%

static char *readcontent(const char *filename)
{
    char *fcontent = NULL, c;
    int index = 0, pagenum = 1;
    FILE *fp;
    fp = fopen(filename, "r");

    if(fp) {
        while((c = getc(fp)) != EOF) {
            if(!fcontent || index == PAGE_SIZE) {
                fcontent = (char*) realloc(fcontent, PAGE_SIZE * pagenum + 1);
                ++pagenum;
            }
            fcontent[index++] = c;
        }
        fcontent[index] = '\0';
        fclose(fp);
    }
    return fcontent;
}

main( argc, argv) int argc; char** argv;
{
	
       int ret;
       int cflags = REG_EXTENDED;
       char* str = readcontent(argv[1]);
       regex_t reg;
       regex_t reg2;
       int match_num = 0;
       FILE* f1 = fopen("operate.txt","w");

       // reg compile
       ret = regcomp(&reg, "if[ \\t]*\\([0-9a-zA-Z]*[>|<|+|/|-|*][0-9a-zA-Z]*\\)", cflags);
       if (ret != 0) {
             char errStr[128];
             regerror(ret, &reg, errStr, sizeof(errStr));
             printf("regcomp error(%s)\n", errStr);
             exit(1);
       }

       int eflags = 0, len;
       int offset = 0, cnt = 1;
       // pattern matching
       regmatch_t pmatch;
       while((ret = regexec(&reg, str+offset, MATCH_SIZE, &pmatch, eflags)) == 0)
       {
       	     match_num += 1;
             len = pmatch.rm_eo - pmatch.rm_so;
	     printf("matched string(%d:%.*s)\n",cnt, len, str+offset+pmatch.rm_so);
	     printf("%.*s\n", len-4, str+offset+pmatch.rm_so+3);
             fprintf(f1,"%.*s\n", len-4, str+offset+pmatch.rm_so+3);
	     offset += pmatch.rm_eo;
             cnt++;
       }
       regfree(&reg);


       ret = regcomp(&reg, "while[ \\t]*\\([0-9a-zA-Z]*[>|<|+|/|-|*][0-9a-zA-Z]*\\)", cflags);
       if (ret != 0) {
             char errStr[128];
             regerror(ret, &reg, errStr, sizeof(errStr));
             printf("regcomp error(%s)\n", errStr);
             exit(1);
       }

       eflags = 0;
       len = 0;
       offset = 0;
       cnt = 1;
       // pattern matching
       while((ret = regexec(&reg, str+offset, MATCH_SIZE, &pmatch, eflags)) == 0)
       {
	     match_num += 1;
             len = pmatch.rm_eo - pmatch.rm_so;
	     printf("matched string(%d:%.*s)\n",cnt, len, str+offset+pmatch.rm_so);
             printf("%.*s\n", len-7, str+offset+pmatch.rm_so+6);
	     fprintf(f1,"%.*s\n", len-7, str+offset+pmatch.rm_so+6);
	     offset += pmatch.rm_eo;
             cnt++;
       }
       regfree(&reg);

       fclose(f1);

       f1 = fopen("operate.txt","r");
       yyin = f1;
       for(int i=1; i<= match_num; i++)
       {
       		yyparse();
       }
       fclose(f1);
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).