使用fparsec

时间:2015-05-30 23:54:07

标签: f# fparsec

Noob警告!

好的,我试图在fparsec中构建一个简单的数学表达式解析器。现在我想要它做的就是处理这样的字符串" 1 + 2-3 * 4/5"并且作为评估结果返回一个double。没有空格,换行符或parens,从左到右的操作顺序都可以。

这是我到目前为止所拥有的:

let number = many1 digit |>> fun ds -> int <| String.Concat(ds)

let op : Parser<int -> int -> int, unit> =
    charReturn '+' (+) <|>
    charReturn '-' (-) <|>
    charReturn '*' (*) <|>
    charReturn '/' (/)

let expression, expressionImpl = createParserForwardedToRef()
do expressionImpl :=
    choice[
        attempt(number .>> op >>. expression);
        number]

let test p str =
    match run (p .>> eof) str with
    | Success(result, _, _) -> printfn "Success: %A" result
    | Failure(result, _, _) -> printfn "Failure: %A" result

[<EntryPoint>]
let main argv = 

    test expression "1+1/2*3-4"
    Console.Read() |> ignore

    0

在表达式解析器的第一个选择中,我不确定如何应用op解析器返回的函数。

1 个答案:

答案 0 :(得分:0)

像往常一样,我在发布问题后找到了答案(经过3个小时的搜索)。

我刚刚更改了这一行:

attempt(pipe3 number op expression (fun x y z -> y x z));

到此:

/******************************************************************************/
// First create and initialize two NSMutableArrays. One for accel data and one
// for gyro data. Then create and initialize CMMotionManager.  Finally,
// call this function

- (void) TestRawSensors
{
   speedTest = 0.0001; // Lets try 10,000Hz
   motionManager.accelerometerUpdateInterval = speedTest;
   motionManager.gyroUpdateInterval = speedTest;


    [motionManager startAccelerometerUpdatesToQueue: [NSOperationQueue currentQueue]
    withHandler: ^(CMAccelerometerData  *accelerometerData, NSError *error)
    {
       [rawAccelSpeedTest addObject: [NSNumber numberWithDouble: accelerometerData.timestamp]];
       [rawAccelSpeedTest addObject: [NSNumber numberWithDouble: accelerometerData.acceleration.x]];

       if (error)
       {
          NSLog(@"%@", error);
       }

       if (rawAccelSpeedTest.count > 100)
       {
          [motionManager stopAccelerometerUpdates];

          for (uint16_t i = 0; i < rawAccelSpeedTest.count; i+=2)
          {
             NSLog(@"Time: %f   Accel: %f", [rawAccelSpeedTest[i] doubleValue],
                                            [rawAccelSpeedTest[i+1] doubleValue]);
          }
       }
    }];


   [motionManager startGyroUpdatesToQueue: [NSOperationQueue currentQueue]
                              withHandler: ^(CMGyroData *gyroData, NSError *error)
    {
       [rawGryoSpeedTest addObject: [NSNumber numberWithDouble: gyroData.timestamp]];
       [rawGryoSpeedTest addObject: [NSNumber numberWithDouble: gyroData.rotationRate.x]];

       if (error)
       {
          NSLog(@"%@", error);
       }

       if (rawGryoSpeedTest.count > 100)
       {
          [motionManager stopGyroUpdates];

          for (uint16_t i = 0; i < rawGryoSpeedTest.count; i+=2)
          {
             NSLog(@"Time: %f   Rate: %f", [rawGryoSpeedTest[i] doubleValue],
                                           [rawGryoSpeedTest[i+1] doubleValue]);
          }

       }
    }];
}

但是,我才意识到我的表达式向后解析。回到绘图板。