From: Jonathan Paisley Date: 2005-03-17T02:49:46+09:00 Subject: Re: Examples for racc? On Thu, 17 Mar 2005 01:59:14 +0900, Ben Giddings wrote: > To do that, I want to parse this: > > gcc -D__DEBUG__ -I../include -I/foo/bar/path -funroll-loops -o foo foo.c > > to this: > > # @includes=["../include", "/path/to/foo"], @defines=["DEBUG"], > @sources=["foo.c"], @output="foo"> > > In other words, I want to make an object with the parsed commandline. Perhaps it is sufficient to scan the line a couple of times with some regular expressions? e.g.: class GCCInvocation def initialize(line) @includes = [] @lang_opts = [] @output = "" @defines = [] @sources = [] line = " " + line line.gsub!(/ -I ?(\S+)/) { @includes << $1; "" } line.gsub!(/ -f ?(\S+)/) { @lang_opts << $1; "" } line.gsub!(/ -o ?(\S+)/) { @output = $1; "" } line.gsub!(/ -D ?(\S+)/) { @defines << $1; "" } line.gsub!(/ ([^- ]\S+)/) { @sources << $1 } @remainder = line.squeeze(' ') end end while line = gets case line when /^gcc (.*)$/ p GCCInvocation.new($1) end end