r/dailyprogrammer • u/Steve132 0 1 • Jul 12 '12
[7/12/2012] Challenge #75 [difficult] (C Preprocessor)
First off, I'd like to apologize for posting this 12 hours late, I'm a little new to my mod responsibilities. However, with your forgiveness, we can go onward!
Everyone on this subreddit is probably somewhat familiar with the C programming language. Today, all of our challenges are C themed! Don't worry, that doesn't mean that you have to solve the challenge in C.
For the difficult challenge, we are going to look at the C Pre-Processor. The C pre-processor is a program (implemented as a compilation pass in gcc, but it used to be a standalone program) that reads in text files that have been annotated with special 'commands'. It interprets these commands and uses them to output a transformed version of the text file with no annotations or comments. For some examples, look here and here
Your task is to implement as much of the C preprocessor as you can in as few lines as you can. Obviously getting an implementation fully-conformant with the full specification is very difficult, so try to get the most useful and obvious stuff as close as possible first. At least try to implement comments, #ifdef, #ifndef, #else, #elif, #endif, #define for constants, and #include . Don't kill yourself worrying about studying the spec for strange corner cases, just implement it as close as you can..this is for fun and learning, remember!
More complex stuff like #if and defined() and full macro with arguments support, and other things is a bonus.
As an example, consider this input file:
#define DOGNAME Brian
The following phrase is often used in web design:
#define FRENCH
#ifdef FRENCH //if french
Le renard brun saute par-dessus le chien paresseux. Le nom du chien est DOGNAME.
#else
The brown fox jumped over the lazy dog. The dog's name is DOGNAME.
#endifshould output
The following phrase is often used in web design:
Le renard brun saute par-dessus le chien paresseux. Le nom du chien est Brian.3
u/devilsassassin Jul 13 '12 edited Jul 13 '12
In Java, it doesn't do anything besides spit vars out, but all different parts work:
public static void define(String s) {
String[] line = s.split("\\s+");
if (line.length <= 2) {
varmap.put(line[1], "true");
} else {
StringBuilder sb = new StringBuilder();
for (int i = 2; i < line.length; i++) {
sb.append(line[i]);
sb.append(" ");
}
varmap.put(line[1], sb.toString());
}
}
public static void ifdef(Scanner scan) {
StringBuilder sb = new StringBuilder();
String result = scan.nextLine();
while (scan.hasNextLine() && !result.contains("#else") && !result.contains("#elif") && !result.contains("#endif")) {
if (result.contains("#define")) {
define(result);
} else {
String[] line = result.split("\\s+");
for (int i = 0; i < line.length; i++) {
String res = line[i].substring(0, line[i].length() - 1);
if (varmap.containsKey(res)) {
sb.append(varmap.get(res));
sb.append(line[i].charAt(res.length()));
sb.append(" ");
} else if (varmap.containsKey(line[i])) {
sb.append(varmap.get(line[i]));
sb.append(" ");
} else {
sb.append(line[i]);
sb.append(" ");
}
}
sb.append(System.lineSeparator());
}
result = scan.nextLine();
}
sop(sb.toString());
}
public static void include(String s,String current) {
String[] line = s.split("\\s+");
String included = infilep + line[1];
if(!included.equals(current)){
cproc(included);
}
}
static HashMap<String, String> varmap = new HashMap<>();
public static String cproc(String infile) {
StringBuilder sb = new StringBuilder();
InputStream fis = null;
try {
fis = new FileInputStream(infile);
Scanner scan = new Scanner(fis);
while (scan.hasNextLine()) {
String result = scan.nextLine();
if (result.contains("#include")) {
include(result,infile);
}
if (result.contains("#define")) {
define(result);
}
if (result.contains("#ifdef") || result.contains("#ifndef")) {
String[] line = result.split("\\s+");
if (varmap.containsKey(line[1])) {
ifdef(scan);
} else {
while (scan.hasNextLine() && !result.contains("#else") && !result.contains("#elif")) {
result = scan.nextLine();
line = result.split("\\s+");
if (result.contains("#elif") && !varmap.containsKey(line[1])) {
result = scan.nextLine();
}
}
ifdef(scan);
}
}
}
} catch (IOException ex) {
Logger.getLogger(Huffer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Huffer.class.getName()).log(Level.SEVERE, null, ex);
}
}
return sb.toString();
}input files:
#include cproclink.txt
#define DOGNAME Brian
The following phrase is often used in web design:
#define FRENCHY
#ifdef FRENCH //if french
Le renard brun saute par-dessus le chien paresseux. Le nom du chien est DOGNAME.
#else
The brown fox jumped over the lazy dog. The dog's name is DOGNAME And he eats with HATNAME.
#endif
(this is the file cproclink.txt)
#define HATNAME Mark
#define ENGLISH
#ifndef FRENCH //if french
Le renard brun saute par-dessus le chien paresseux. Le nom du chien est HATNAME.
#elif ENGLISH
HATNAME.
#else
The brown fox jumped over the lazy dog. The dog's name is HATNAME.
#endifoutput:
Mark.
The brown fox jumped over the lazy dog. The dog's name is Brian And he eats with Mark . 1
u/sleepingsquirrel Jul 12 '12
Perl -- Down and dirty. Just enough implemented for the example.
#!/usr/bin/perl -w
use strict;
my $text = do{local $/; <>}; #slurp from STDIO
#strip comments, broken for comments in quotation marks
$text =~ s{//[^\n]*\n}{}g;
$text =~ s{/\*.*?\*/}{}sg;
my $identifier = qr/[_a-zA-Z][_a-zA-Z0-9]*/;
my $define = qr/#\s*define\s+($identifier)[ \t]*([^\n]*?)\n/;
my $ifdef_else = qr/#\s*ifdef\s+($identifier)\s+(.*?)#\s*else\s(.*?)#\s*endif\s/s;
my %macros;
print cpp($text);
sub cpp
{
my $t = $_[0];
$t =~ s/($identifier)|$define|$ifdef_else/
defined($1) ? (defined($macros{$1})?$macros{$1}:$1) :
(defined($2) ? ($macros{$2}=$3,"") :
(defined($4) ? (exists($macros{$4})?cpp($5):cpp($6)):""))
/egs;
return $t;
}
2
u/[deleted] Jul 12 '12
I don't understand this challenge... Implement as much of the C preprocessor in as few lines as you can? What do you mean 'Implement the C preprocessor'?