Dynamic PEG for interpreted languages.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

96 lignes
3.1 KiB

#include <stdio.h>
#include <stdlib.h>
void setBit(char *bits, int n)
{
bits[n/8] |= (1 << n%8);
}
int testBit(char *bits, int n)
{
return bits[n/8] & (1 << n%8);
}
char *classify(char *spec)
{
char *class= malloc(32); // 256 bits indicating if character N (bit position N) is in the class or not
int index=0;
while (spec[index] != '\0' ) { //*spec
// go through spec converting each entry in the spec into a set of bits in the class
// test if the next thing is \ .
// or test if you have X-Y (where X or Y might be a character or escaped character)
// \n-\r
if (spec[index]=='-' && index!=0 && !(index==1 && spec[0]=='^')){
if(spec[index+1]=='\0'){
printf("Error, bad use of range\n");
}
if(spec[index-1]<65 || spec[index+1]>122 || (spec[index-1]>90 && spec[index-1]<97) || (spec[index+1]>90 && spec[index+1]<97)){
//= if(spec[index-1] or spec[index+1] are not letters)
if((spec[index-1]>47 && spec[index-1]<57) || (spec[index+1]>47 && spec[index+1]<57) ){ //if it's a digit range
if(spec[index-1]>spec[index+1]){
printf("Error, first digit greater than the second one\n");
}
else{
for(int j=spec[index-1]; j<=spec[index+1] ; j++){
setBit(class,j);
printf("added digit : %c\n",j);
}
}
}
else{
printf("Error, aekzaepaz");
}
}
else{
if(spec[index-1]>spec[index+1]){
printf("Error, first char greater than the second one");
}
else if(spec[index-1]>=65 && spec[index-1]<=90 && spec[index+1]>=65 && spec[index+1]<=90 ){
for(int j=spec[index-1]; j<=spec[index+1] ; j++){
setBit(class,j);
printf("added char : %c\n",j);
}
}
else if( spec[index-1]>=97 && spec[index-1]<=122 && spec[index+1]>=97 && spec[index+1]<=122 ){
for(int j=spec[index-1]; j<=spec[index+1] ; j++){
setBit(class,j);
printf("added char : %c\n",j);
}
}
}
}
printf("%i\n",spec[index]);
index++;
}
if (spec[0]=="^") {
for (int i= 0; i < 8; ++i) class[i] ^= 255; // invert all bits in the class
}
return class;
}
int main()
{
char *line=0;
size_t line_max=0;
ssize_t line_len=0;
char a='a';
printf("%i\n",(int)a);
printf("%s",classify("^-abca"));
while ((line_len= getline(&line,&line_max,stdin)) >= 0) {
if (line_len>0 && line[line_len-1]=='\n') {
line[line_len-1]=0;
}
char *class= classify(line);
for (int i= 0; i < 256; ++i)
if (testBit(class, i))
printf("%02x is set\n", i);
}
return 0;
}